cut a string in Go
Code snippet on how to cut a string in Go
s := "This is a string"
parts := strings.Split(s, " ")
fmt.Println(parts[:2])
This code snippet uses the strings.Split
function to cut a string in the programming language Go. The first line creates a string called s
and stores the text “This is a string” in it. Next, the strings.Split
function is used to separate s
into multiple pieces, based on the delimiter ” ” (space). The result of this function call is stored in parts
which contains an array of strings. Lastly, the fmt.Println
function is used to print the contents of parts
up to the second element, which will output:
[This is]