fmt.Println(foo)
fmt.Println(bar)

  fmt.Printf(foo \\\
bar\\ \\ n)

这两行都打印出相同的两行,如下所示:

  fmt.Println(foo\\\
bar)


I am trying read certain string output generated by linux command by the following code:

out, err := exec.Command("sh", "-c", cmd).Output()
[]byte
strings.Split(output, "\n")

and

bufio.NewScanner(strings.NewReader(output))

but they both split the whole string buffer whenever seeing a "\n" character.


Print first result: "123;\n234;\n"
Print second result: "456;\n"

The whole output is one big multi-line string, it may also contain some other quoted strings, and I am processing the whole string output in my go program, but I can't control the command output and add a back slash before the "\n" character.

\n\n
First line: "test1"
Second line: "123;\n234;\n345;"
Third line: "456;\n567;"
Fourth line: "test4"
for line in f

There is no distinction between a "real" and an "unreal" line break.

'\n''\n'
'\n'"foo\nbar\n""foo""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")

这篇关于Go lang区分“\ n”和换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!