Go中的fmt.Println()和println()之间的区别 [英] Difference between fmt.Println() and println() in Go
问题描述
如下图所示,fmt.Println()
和println()
在Go中给出相同的输出:Hello world!
As illustrated below, both fmt.Println()
and println()
give same output in Go: Hello world!
但是:它们彼此之间有何不同?
But: how do they differ from each other?
片段1,使用 fmt
包;
Snippet 1, using the fmt
package;
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello world!")
}
代码段2,不包含fmt
软件包;
Snippet 2, without the fmt
package;
package main
func main() {
println("Hello world!")
}
推荐答案
println
是内置函数(在运行时),最终可能会被删除,而fmt
包位于标准库中,该库将持续存在.有关该主题,请参见规范.
println
is an built-in function (into the runtime) which may eventually be removed, while the fmt
package is in the standard library, which will persist. See the spec on that topic.
对于语言开发人员来说,拥有一个没有依赖关系的println
很方便,但是方法是使用fmt
包或类似的东西(例如log
).
For language developers it is handy to have a println
without dependencies, but the way to go is to use the fmt
package or something similar (log
for example).
您可以参见实现 print(ln)
函数的设计目的甚至不是为了远程支持其他输出模式,而主要是调试工具.
As you can see in the implementation the print(ln)
functions are not designed to even remotely support a different output mode and are mainly a debug tool.
这篇关于Go中的fmt.Println()和println()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!