Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 576
carbocation Avatar asked Jan 24 '13 04:01

carbocation


People also ask

How do you enter a newline?

Move the text cursor to where you want the new line to begin, press the Enter key, hold down the Shift key, and then press Enter again. You can continue to press Shift + Enter to move to each new line, and when ready to move to the next paragraph, press Enter .

What is\ n in golang?

The escape character \t denotes a horizontal tab and \n is a line feed or newline.


1 Answers

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.

like image 103
dskinner Avatar answered Oct 18 '22 09:10

dskinner