在go / golang中代表换行符的最便携/跨平台的方式是什么? [英] What is the most portable/cross-platform way to represent a newline in go/golang?
问题描述
目前,为了表示go程序中的新行,我使用 \\\
。例如:
包主
导入fmt
func main(){
fmt.Printf(%d是%s \\\
,'U',string(85))
}
...将产生 85是U
,然后是换行符。
然而,这似乎并不是跨平台的。查看其他语言,PHP用全局常量( PHP_EOL
)表示它。
我对此感到好奇,所以决定看看 fmt.Println
完成了什么工作。 http://golang.org/src/pkg/fmt/print.go
如果您滚动至底部,您将看到 if addnewline
其中 \\\
总是被使用。我几乎不会说这是否是最跨平台的方式,而go最初是与linux绑定的,但这就是std lib的原因。
我原本打算建议使用 fmt.Fprintln
,这可能仍然有效,就好像当前的功能不合适,错误可能会被提交,然后代码只需要用最新的Go工具链进行编译。
Currently, to represent a newline in go programs, I use \n
. For example:
package main
import "fmt"
func main() {
fmt.Printf("%d is %s \n", 'U', string(85))
}
... will yield 85 is U
followed by a newline.
However, this doesn't seem all that cross-platform. Looking at other languages, PHP represents this with a global constant ( PHP_EOL
). Is \n
the right way to represent newlines in a cross-platform specific manner in go / golang?
I got curious about this so decided to see what exactly is done by fmt.Println
. http://golang.org/src/pkg/fmt/print.go
If you scroll to the very bottom, you'll see an if addnewline
where \n
is always used. I can't hardly speak for if this is the most "cross-platform" way of doing it, and go was originally tied to linux in the early days, but that's where it is for the std lib.
I was originally going to suggest just using fmt.Fprintln
and this might still be valid as if the current functionality isn't appropriate, a bug could be filed and then the code would simply need to be compiled with the latest Go toolchain.
这篇关于在go / golang中代表换行符的最便携/跨平台的方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!