英文:
Difference between fmt.Println() and println() in Go
问题
如下所示,无论是fmt.Println()
还是println()
在Go中都会输出相同的结果:Hello world!
但是:它们之间有什么区别呢?
代码片段1,使用fmt
包;
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello world!")
}
代码片段2,不使用fmt
包;
package main
func main() {
println("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?
Snippet 1, using the fmt
package;
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello world!")
}
Snippet 2, without the fmt
package;
package main
func main() {
println("Hello world!")
}
答案1
得分: 171
println
是一个内置函数(嵌入到运行时中),可能最终会被移除,而fmt
包是标准库中的,将会持续存在。有关此主题,请参阅规范。
对于语言开发者来说,拥有一个没有依赖的println
是很方便的,但更好的方法是使用fmt
包或类似的东西(例如log
)。
正如您可以在实现中看到的,print(ln)
函数并不设计支持不同的输出模式,主要是用作调试工具。
英文:
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.
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).
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.
答案2
得分: 144
To build upon nemo's answer:
println
是一种内置于语言中的函数。它在规范的引导部分中。从链接中可以看到:
> 当前的实现提供了几个在引导过程中有用的内置函数。这些函数为了完整性而被记录,但不能保证会一直存在于语言中。它们不返回结果。
>
> 函数 行为
>
> print 打印所有参数;参数的格式化是与实现相关的
> println 类似于print,但在参数之间打印空格,并在末尾打印换行符
因此,它们对开发人员很有用,因为它们没有依赖项(内置于编译器中),但在生产代码中不适用。还需要注意的是,print
和println
报告给stderr
,而不是stdout
。
然而,fmt
提供的函数族是为了在生产代码中使用的。它们可预测地报告给stdout
,除非另有指定。它们更加灵活(fmt.Fprint*
可以报告给任何io.Writer
,比如os.Stdout
、os.Stderr
,甚至是net.Conn
类型),并且不是与实现相关的。
大多数负责输出的包都依赖于fmt
,比如log
。如果你的程序在生产环境中要输出任何内容,那么fmt
很可能是你想要的包。
英文:
To build upon nemo's answer:
println
is a function built into the language. It is in the Bootstrapping section of the spec. From the link:
<!-- language-all: none -->
> Current implementations provide several built-in functions useful
> during bootstrapping. These functions are documented for completeness
> but are not guaranteed to stay in the language. They do not return a
> result.
>
> Function Behavior
>
> print prints all arguments; formatting of arguments is implementation-specific
> println like print but prints spaces between arguments and a newline at the end
Thus, they are useful to developers, because they lack dependencies (being built into the compiler), but not in production code. It also important to note that print
and println
report to stderr
, not stdout
.
The family provided by fmt
, however, are built to be in production code. They report predictably to stdout
, unless otherwise specified. They are more versatile (fmt.Fprint*
can report to any io.Writer
, such as os.Stdout
, os.Stderr
, or even a net.Conn
type.) and are not implementation specific.
Most packages that are responsible for output have fmt
as a dependency, such as log
. If your program is going to be outputting anything in production, fmt
is most likely the package that you want.
答案3
得分: 7
我可以看到这里的区别:
rangeOverIntsAndStrings(1, 5)
func rangeOverIntsAndStrings(args ...interface{}) {
for _, v := range args {
println(v)
}
}
// 输出
(0x108f060,0x10c5358)
(0x108f060,0x10c5360)
对比
func rangeOverIntsAndStrings(args ...interface{}) {
for _, v := range args {
fmt.Println(v)
}
}
// 输出
1
5
英文:
I can see difference here:
rangeOverIntsAndStrings(1, 5)
func rangeOverIntsAndStrings(args ...interface{}) {
for _, v := range args {
println(v)
}
}
// output
(0x108f060,0x10c5358)
(0x108f060,0x10c5360)
vs
func rangeOverIntsAndStrings(args ...interface{}) {
for _, v := range args {
fmt.Println(v)
}
}
// output
1
5
答案4
得分: -1
有一些println
和fmt.Printf
之间的差异。
英文:
Interesting Example:
➜ netpoll git:(develop) ✗ cat test.go
package main
import "fmt"
func main() {
a := new(struct{})
b := new(struct{})
println(a, b, a == b)
c := new(struct{})
d := new(struct{})
fmt.Printf("%v %v %v\n", c, d, c == d)
}
➜ netpoll git:(develop) ✗ go run test.go
0xc000074f47 0xc000074f47 false
&{} &{} true
➜ netpoll git:(develop) ✗ go run -gcflags="-m" test.go
# command-line-arguments
./test.go:12:12: inlining call to fmt.Printf
./test.go:6:10: new(struct {}) does not escape
./test.go:7:10: new(struct {}) does not escape
./test.go:10:10: new(struct {}) escapes to heap
./test.go:11:10: new(struct {}) escapes to heap
./test.go:12:35: c == d escapes to heap
./test.go:12:12: []interface {} literal does not escape
<autogenerated>:1: .this does not escape
0xc000074f47 0xc000074f47 false
&{} &{} true
It is something difference between println
and fmt.Printf
.
答案5
得分: -2
关于区别,这个是一个例子。
println()
打印一个指向函数 test 的地址的指针。
fmt.Println()
打印函数的地址。
英文:
As for the difference, this is an example.
println()
prints a pointer point to the address of function test.
fmt.Println()
prints the address of function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论