dongpi9494
2016-12-30 06:25 阅读 63
已采纳

为什么在使用`log.Println`和`fmt.Println`时改变输出顺序?

Here is my code: package main

import "log"
import "fmt"

func main() {
    var a string = "initail"
    log.Println(a)
    var b, c int = 1, 2
    fmt.Println(b, c)
}

The output is:

1 2
2016/12/30 14:22:58 initail

So i don't understand why the output's order? why log.Println is slower than fmt.Println?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dqu94359 dqu94359 2016-12-30 06:42

    Only difference between those in terms of its printing behaviour is

    • log.Println writes to Stderr
    • fmt.Println writes to Stdout

    Both are not buffered. So the fact that StdOut came before StdError is specific to your terminal or environment.

    Here is a play link https://play.golang.org/p/0cukg_a9GR

    点赞 评论 复制链接分享

相关推荐