I am trying read certain string output generated by linux command by the following code:
out, err := exec.Command(\"sh\", \"-c\", cmd).Output()
There is no distinction between a "real" and an "unreal" line break.
If you're using a Unix-like system, the end of a line in a text file is denoted by the LF or '\n'
character. You cannot have a '\n'
character in the middle of a line.
A string in memory can contain as many '\n'
characters as you like. The string "foo\nbar\n"
, when written to a text file, will create two lines, "foo"
and "bar"
.
There is no effective difference between
fmt.Println("foo")
fmt.Println("bar")
and
fmt.Printf("foo\nbar\n")
Both print the same sequence of 2 lines, as does this:
fmt.Println("foo\nbar")
It's possible that your "\n" is actually the escaped version of a line break character. You can replace these with real line breaks by searching for the escaped version and replacing with the non escaped version:
strings.Replace(sourceStr, `\n`, "\n", -1)
Since string literals inside backticks can be written over multiple lines, Go escapes any line break characters it sees.