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

huangapple go评论83阅读模式
英文:

why the output order is changed when using `log.Println` and `fmt.Println`?

问题

这是我的代码:

包 main

导入 "log"
导入 "fmt"

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

输出结果是:

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

所以我不明白为什么输出的顺序是这样的?
为什么 log.Printlnfmt.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

得分: 4

这两者在打印行为方面的唯一区别是:

  • log.Println 输出到 Stderr
  • fmt.Println 输出到 Stdout

两者都不带缓冲。所以 StdOutStdError 之前输出的情况是特定于你的终端或环境的。

这是一个示例链接:https://play.golang.org/p/0cukg_a9GR

英文:

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

huangapple
  • 本文由 发表于 2016年12月30日 14:25:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/41391621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定