Iterate over multiline string in Go

In Python I can iterate over a multiline string.

x = """\
this is
my multiline
string!"""

for line in x.splitlines():
    print(line)

Can Go do the same?

You can use bufio.Scanner in Go which iterates over lines from an io.Reader. The following example creates a reader from the given multiline string and passes it to the scanner factory function. Each invocation of scanner.Scan() splits the reader on the next line and buffers the line. If there is no more lines it returns false. Calling scanner.Text() returns the buffered split.

var x string = `this is
my multiline
string`

scanner := bufio.NewScanner(strings.NewReader(x))
for scanner.Scan() {
    fmt.Println(scanner.Text())
}

In the example, the for loop will continue until Scan() returns false at the end of the multiline string. In each loop we print the line returned by the scan.

https://play.golang.org/p/U9_B4TsH6M