Is it safe for more than one goroutine to print to stdout?

前端 未结 3 1376
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 08:39

I have multiple goroutines in my program, each of which makes calls to fmt.Println without any explicit synchronization. Is this safe (i.e., will each line appe

3条回答
  •  独厮守ぢ
    2020-12-09 09:22

    The common methods (fmt.printLine) are not safe. However, there are methods that are.

    log.Logger is "goroutine safe": https://golang.org/pkg/log/#Logger

    Something like this will create a stdout logger that can be used from any go routine safely.

    logger := log.New(os.Stdout, "", 0)

提交回复
热议问题