go有一个内置的“print”函数吗?

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

go has a built in "print" function?

问题

我今天遇到了一些代码,其中有一个未定义的“print”。经过一番尝试,我发现可以使用print将内容输出到控制台。

例如:

 print("Hello World")

所以它似乎是一种内置函数,但我找不到任何相关的参考(而且我以为Go语言规则是小写函数从不被导入)。

这个功能是众所周知的吗?如果是的话,是否还有其他方便的函数,还是我只是非常非常困惑?

另外一点是,这个print函数不使用fmt.Printf的魔术格式化技巧(%v)。如果你打印maps或structs,你似乎会得到它们的地址。

英文:

I cam across some code today that suprised me with a 'print' that wasn't defined. After a little playing I determined that you can just use a print to get things dumped to the console

e.g.

 print("Hello World")

So it seems to be some sort of builtin but I can't find any reference to it (and I thought the go rules were lowercase functions never imported anyway)

Is this well known and if so are there other convenience functions or am I just very, very confused?

One other point -- this print doesn't use the magic formatting tricks (%v) of fmt.Printf -- If you print maps or structs you seem to get their address.

答案1

得分: 13

printprintln在这里被定义:http://golang.org/ref/spec#Predeclared_identifiers

它们的目的在这里被解释:http://golang.org/ref/spec#Bootstrapping

英文:

print and println are defined here.

Their purpose is explained here.

答案2

得分: 13

你是对的,还有其他人已经抱怨过这个问题。它已经被添加到下一个Go版本(go1.2)的内置文档中。

> 内置包
>
> 函数 print
>
> func print(args ...Type)
>
> print 内置函数以特定于实现的方式格式化其参数,并将结果写入标准错误输出。print函数对于引导和调试很有用,但不能保证会一直存在于语言中。
>
> 函数 println
>
> func println(args ...Type)
>
> println 内置函数以特定于实现的方式格式化其参数,并将结果写入标准错误输出。参数之间总是添加空格,并附加一个换行符。println函数对于引导和调试很有用,但不能保证会一直存在于语言中。

英文:

You are right, and someone else has already complained about it. It's been added to the built-in documentation for the next Go release (go1.2).

> Package builtin
>
> func print
>
> func print(args ...Type)
>
> The print built-in function formats its arguments in an
> implementation-specific way and writes the result to standard error.
> Print is useful for bootstrapping and debugging; it is not guaranteed
> to stay in the language.
>
> func println
>
> func println(args ...Type)
>
> The println built-in function formats its arguments in an
> implementation-specific way and writes the result to standard error.
> Spaces are always added between arguments and a newline is appended.
> Println is useful for bootstrapping and debugging; it is not
> guaranteed to stay in the language.

huangapple
  • 本文由 发表于 2013年7月17日 04:15:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/17686126.html
匿名

发表评论

匿名网友

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

确定