fmt.Println()和println()之间有什么区别 [英] what's the difference between fmt.Println() and println() in go

查看:936
本文介绍了fmt.Println()和println()之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  importfmt$ b在进行中,如果我们要打印某些内容,可以这样做: $ b func main(){
fmt.Println(Hello world!)
}

但我发现可以在不导入 fmt 的情况下执行相同的操作:

  func main(){
println(Hello world!)
}

有人可以解释吗?

解决方案

println 是一个内置函数(进入运行时),可能会消失,而 fmt 程序包位于标准库中,该函数将持续存在。有关该主题,请参阅规范



对于语言开发人员,在没有依赖的情况下有一个 println 方便,但是要使用 fmt 包或类似的东西 log 例如)。



正如你可以在实施中查看 print(ln)函数是不设计甚至远程支持不同的输出模式,主要是调试工具。


In go, if we want to print something, we can do so as follows:

import "fmt"
func main(){
    fmt.Println("Hello world!")
}

But I found that one can do the same without importing fmt:

func main(){
    println("Hello world!")
}

Could someone please explain?

解决方案

println is an inbuilt function (into the runtime) that may go away, 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.

这篇关于fmt.Println()和println()之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆