In Go, you can split a string by new line using the strings.Split
function with the newline delimiter "\n"
.
In this example,
str
with a value containing new lines.strings.Split
function with the newline delimiter "\n"
to split the string into an array of substrings.package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello\nWorld\n"
substrings := strings.Split(str, "\n")
for _, sub := range substrings {
fmt.Println(sub)
}
}
Hello World
In this tutorial, we learned How to Split a String by New Line in Go language with well detailed examples.