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

后端 未结 3 1426
深忆病人
深忆病人 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:53

    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.

提交回复
热议问题