Go lang differentiate “\n” and line break

后端 未结 2 770
旧巷少年郎
旧巷少年郎 2021-01-06 05:29

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

out, err := exec.Command(\"sh\", \"-c\", cmd).Output()
2条回答
  •  天命终不由人
    2021-01-06 05:58

    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.

提交回复
热议问题