log包是go语言提供的一个简单的日志记录功能,其中定义了一个结构体类型 Logger,是整个包的基础部分,包中的其他方法都是围绕这整个结构体创建的. Logger结构 Logger结构的定义如下: type Logger struct { mu sync.Mutex prefix string flag int out io.Writer buf []byte } mu 是sync.Mutex,它是一个同步互斥锁,用于保证日志记录的原子性. prefix 是输入的日志每一行的前缀 flag 是…
Go标准库中提供了Sync.Once来实现"只执行一次"的功能.学习了一下源代码,里面用的是经典的双重检查的模式: // Once is an object that will perform exactly one action. type Once struct { m Mutex done uint32 } func (o *Once) Do(f func()) { if atomic.LoadUint32(&o.done) == 1 { return } // Slow…
在开发过程中可能会遇到这样的情况,有一些包是引入自不同地方的,比如: golang.org/x/net/html 和 net/html, golang.org/x/crypto 和 crypto. 那这是什么原因呢? 引用 Go 官方 Wiki 的说法: The golang.org/x/... repositories are part of the Go Project but outside the main Go tree. They are developed under looser…