til/content/posts/golang-split-by-space.md

551 B

title date draft tags images
Split on spaces in Go 2020-09-18 false
go
golang
/images/golang-split-by-space.gif

While looking to split a multiline and space separated string and not having any look with strings.Split() I came across this somewhat oddly names function:

import (
    "fmt"
    "strings"
)

func main() {
    input := `This is 
a multiline, space 
separated string`

    output := strings.Fields(input)

    fmt.Println(output) // ["This", "is", "a", "multiline,", "space", "separated", "string"]
}