Go 标准包阅读
基于 Go 1.10.1
strings
- builder.go
- builder_test.go
- compare.go
- compare_test.go
- example_test.go
- export_test.go
- reader_test.go
- reader.go
问题清单
以下是我们在阅读过程中的一些问题,希望可以引起大家的关注,也欢迎大家提出自己的理解,最好可以给以文章总结。
- defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
- runtime.ReadMemStats(&m1)
- defer_lock.go
package main
import (
"sync"
)
func main() {
var mu sync.Locker = new(I)
defer LockUnlock(mu)()
println("doing")
}
func LockUnlock(mu sync.Locker) (unlock func()) {
mu.Lock()
return mu.Unlock
}
type I struct{}
func (i *I) Lock() {
println("lock")
}
func (i *I) Unlock() {
println("unlock")
}
-
buf := make([]byte, len(b.buf), 2*cap(b.buf)+n) 为什么是2倍呢?
-
// NOTE(rsc): This function does NOT call the runtime cmpstring function,
// because we do not want to provide any performance justification for
// using strings.Compare. Basically no one should use strings.Compare.
// As the comment above says, it is here only for symmetry with package bytes.
// If performance is important, the compiler should be changed to recognize
// the pattern so that all code doing three-way comparisons, not just code
// using strings.Compare, can benefit. -
b.buf = append(b.buf, s…) s是string,b.buf是[]byte
-
int int64的问题? 在32位机器上进行int64原子操作时的panic
-
defer LockUnlock(mu),如果LockUnlock(mu)没有带(),则会丢失func函数的执行
-
if r < utf8.RuneSelf
-
if cap(b.buf)-l < utf8.UTFMax {
-
Example
-
xxx_test.go 其实是xxx包,但是又不想放到xxx包里面,因为它只是提供给_test.go包使用的函数。
-
rune 码点的处理(reader.go prevRune int // index of previous rune; or < 0)
-
off 与 offset 尴尬的问题
-
whence where when ?wiki-whence
-
// It is similar to bytes.NewBufferString but more efficient and read-only.
-
Go 1.10 开始引入了 builder
# https://golang.org/doc/go1.10#strings
strings ¶
A new type Builder is a replacement for bytes.Buffer for the use case of accumulating text into a string result. The Builder's API is a restricted subset of bytes.Buffer's that allows it to safely avoid making a duplicate copy of the data during the String method.