25

golang 你所不知道的 log 和 fmt

 4 years ago
source link: https://www.tuicool.com/articles/nMNFfa2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

直接点说,就是由于fmt 是线程不安全的, 如果你在多协程场景下使用fmt打印信息可能会得到乱序的结果就是说 不按代码里的顺序打印.

下面看示例

代码示例

golang fmt 多线程 乱序:

func main()  {
    wg := sync.WaitGroup{}
    wg.Add(10)
    var arr []int

    for i:=0;i<10;i++ {
        go func(i int) {
            defer wg.Done()
            arr = append(arr, i)
            log.Printf("i: %d",i)
        }(i)
    }
    wg.Wait()  // 隔离
    s :=0
    for _, v := range arr{
        fmt.Println(v)
        s += v
    }
    log.Println(s)

}

输出结果是这样的:

2019/09/16 20:54:22 i: 2
2
2019/09/16 20:54:22 i: 4
4
2019/09/16 20:54:22 i: 0
8
2019/09/16 20:54:22 i: 8
7
2019/09/16 20:54:22 i: 6
2019/09/16 20:54:22 40

虽然有wait 隔离了协程的执行,但 fmt 还是导致了乱序. 和普通的多线程问题还不太一样.

这个我和同事研究了一下, 有可能 是因为log 有个输出的buffer,wait之前的逻辑 只能确保信息输出到buffer,之后log 和fmt 竞争std.out. 所以交叉输出.

最后

最后,打印信息的话 一般也不用fmt,fmt一般用来格式化. 所以没啥奇葩需求也就不要用fmt了.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK