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

后端 未结 3 1413
深忆病人
深忆病人 2020-12-30 20:24

Currently, to represent a newline in go programs, I use \\n. For example:

package main

import \"fmt\"


func main() {
    fmt.Printf(\"%d is %s         


        
3条回答
  •  礼貌的吻别
    2020-12-30 20:52

    Having the OS determine what the newline character is happens in many contexts to be wrong. What you are really want to know is what the "record" seperator 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.

提交回复
热议问题