什么是在go/golang中表示换行的最便携/跨平台的方式?

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

What is the most portable/cross-platform way to represent a newline in go/golang?

问题

目前,在Go程序中表示换行符,我使用\n。例如:

package main

import "fmt"


func main() {
    fmt.Printf("%d is %s \n", 'U', string(85))
}

...将输出85 is U后面跟着一个换行符。

然而,这种方式似乎并不是非常跨平台。看看其他语言,PHP使用全局常量(PHP_EOL)来表示换行符。在Go / Golang中,\n是表示换行符的正确方式吗?

英文:

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?

答案1

得分: 27

我对此感到好奇,所以决定看看fmt.Println到底做了什么。http://golang.org/src/pkg/fmt/print.go

如果你滚动到最底部,你会看到一个if addnewline,其中总是使用\n。我不能确定这是否是最“跨平台”的方法,而且Go最初与Linux有关,但这就是标准库的位置。

我最初打算建议只使用fmt.Fprintln,如果当前功能不合适,可以提交一个bug,然后代码只需要使用最新的Go工具链进行编译。

英文:

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.

答案2

得分: 20

在许多情况下,让操作系统确定换行符是什么是错误的。你真正想知道的是“记录”分隔符是什么,而Go假设作为程序员的你应该知道。

即使二进制文件在Windows上运行,它可能会读取来自Unix操作系统的文件。

行尾是由文件或文档的来源所指定的行尾决定的,而不是二进制文件所运行的操作系统。

英文:

Having the OS determine what the newline character is happens in many contexts to be wrong. What you really want to know is what the "record" separator is and Go assumes that you as the programmer should know that.

Even if the binary runs on Windows, it may be consuming a file from a Unix OS.

Line endings are determined by what the source of the file or document said was a line ending, not the OS the binary is running in.

答案3

得分: 19

你可以始终使用特定于操作系统的文件来声明某些常量。就像 _test.go 文件只在执行 go test 时使用一样,_[os].go 文件只在构建到目标平台时包含。

基本上,你需要添加以下文件:

  • main.go
  • main_darwin.go // Mac OSX
  • main_windows.go // Windows
  • main_linux.go // Linux

你可以在每个 main_[os].go 文件中声明一个 LineBreak 常量,并在 main.go 中编写你的逻辑。

你的文件内容应该类似于:

main_darwin.go

package somepkg

const LineBreak = "\n"

main_linux.go

package somepkg

const LineBreak = "\n"

main_windows.go

package somepkg

const LineBreak = "\r\n"

然后,在你的 main.go 文件中,编写代码并引用 LineBreak

main.go

package main

import "fmt"


func main() {
    fmt.Printf("%d is %s %s", 'U', string(85), LineBreak)
}
英文:

You can always use an OS specific file to declare certain constants. Just like _test.go files are only used when doing go test, the _[os].go are only included when building to that target platform.

Basically you'll need to add the following files:

 - main.go
 - main_darwin.go     // Mac OSX
 - main_windows.go    // Windows
 - main_linux.go      // Linux

You can declare a LineBreak constant in each of the main_[os].go files and have your logic in main.go.

The contents of you files would look something like this:

main_darwin.go

package somepkg

const LineBreak = "\n"

main_linux.go

package somepkg

const LineBreak = "\n"

main_windows.go

package somepkg

const LineBreak = "\r\n"

and simply in your main.go file, write the code and refer to LineBreak

main.go

package main

import "fmt"


func main() {
    fmt.Printf("%d is %s %s", 'U', string(85), LineBreak)
}

答案4

得分: 1

你可以使用os.PathSeparator

func main() {
    var PS = fmt.Sprintf("%v", os.PathSeparator)
    var LineBreak = "\n"
    if PS != "/" {
        LineBreak = "\r\n"
    }
    fmt.Printf("Line Break %v", LineBreak)
}
英文:

You can use os.PathSeparator

func main() {
	var PS = fmt.Sprintf("%v", os.PathSeparator)
	var LineBreak = "\n"
	if PS != "/" {
		LineBreak = "\r\n"
	}
	fmt.Printf("Line Break %v", LineBreak)
}

https://play.golang.com/p/UTnBbTJyL9c

huangapple
  • 本文由 发表于 2013年1月24日 12:15:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/14493867.html
匿名

发表评论

匿名网友

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

确定