如何在换行符上分割字符串?

I'm trying to do the rather simple task of splitting a string by newlines.

This does not work:

temp := strings.Split(result,`
`)

I also tried ' instead of ` but no luck.

Any ideas?

You have to use " ".

Splitting on ` `, searches for an actual \ followed by n in the text, not the newline byte.

playground

It does not work because you're using backticks:

Raw string literals are character sequences between back quotes ``. Within the quotes, any character is legal except back quote. The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; in particular, backslashes have no special meaning and the string may contain newlines.

Reference: http://golang.org/ref/spec#String_literals

So, when you're doing

strings.Split(result,`
`)

you're actually splitting using the two consecutive characters "\" and "n", and not the character of line return " ". To do what you want, simply use " " instead of backticks.

For those of us that at times use Windows platform, it can help remember to use replace before split:

strings.Split(strings.Replace(windows, "
", "
", -1), "
")

Go Playground

' doesn't work because it is not a string type, but instead a rune.

temp := strings.Split(result,'
')

go compiler: cannot use '\u000a' (type rune) as type string in argument to strings.Split

definition: Split(s, sep string) []string