Go lang区分“\ n”和换行符 [英] Go lang differentiate "\n" and line break

查看:6124
本文介绍了Go lang区分“\ n”和换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过以下代码读取由linux命令生成的某些字符串输出:

  out,err:= exec。 Command(sh,-c,cmd).Output()

[] byte 类型,我如何区分线内容中包含的\ n字符与真正的换行符?我试过

  strings.Split(输出,\\\

  bufio.NewScanner(strings.NewReader(输出))

但是他们都会在看到\ n字符时分割整个字符串缓冲区。



好的,为了澄清,虚幻中断是一个包含在字符串中的\ n字符,如下所示:

打印第一个结果:123; \\\
234; \\\

打印第二个结果:456; \\\



整个输出是一个大的多行字符串,它也可能包含一些其他引用的字符串,并且我正在处理我的go程序中的整个字符串输出,但是我无法控制命令输出并在\ n字符前添加一个反斜杠。



进一步说明:我打算处理包含字符串串的字节序列,并且要保留包含在内部字符串中的 \\\
并使用外层r \\\
来断线。所以对于下面的字节序列:

 第一行:test1
第二行:123; \\\
234 ; \\\
345;
第三行:456; \\\
567;
第四行:test4

我想在处理整个序列时得到3行,而不是总共7条线。这是一个旧的项目,但我记得我可以使用Python直接使用类似用于f 中的行的语法获得3行,并打印第二个内部字符串的内容而不是呈现它。

解决方案

没有区分真实和虚幻换行符。 b
$ b

如果您使用的是类Unix系统,则文本文件中行的末尾用LF或'\ n'字符。你不能在行的中间有'\ n'字符。



内存中的字符串包含尽可能多的'\\\
'
字符。字符串foo \\\
bar\\\
写入文本文件时,将创建两行foo> bar





  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()

The above out is of []byte type, how can I differentiate the "\n" character contained in line content with the real line break? I tried

strings.Split(output, "\n")

and

bufio.NewScanner(strings.NewReader(output))

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

OK, to clarify, an "unreal" break is a "\n" character contained in a string as follows, 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.

Further clarify: I meant to process byte sequence which contains string of strings, and want to preserve the "\n" contained in the inner string and use the the outer layer "\n" to break lines. So for the following byte sequence:

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

I want to get 3 lines when processing the whole sequence, instead of getting 7 total lines. It's a old project, but I remember I can use Python to directly get 3 lines using syntax like "for line in f", and print the content of second inner string instead of rendering it.

解决方案

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")

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆