OneBite.Dev - Coding blog in a bite size

split a string in Go

Code snippet on how to split a string in Go

  input := "This is an example string"
  output := strings.Split(input, " ")

This code shows how to split a string into separate words using the Go programming language. The first step is to declare a variable called “input” and assign it the value of the string to be split. In this example, the string is “This is an example string”. The second step is to use the Split() function from the strings package. This function takes a string and a character as arguments and returns a string array. The first argument is the input string and the second argument is the character to split on. In this example, we want to divide the string into separate words, so we split on the space character. The last step is to assign the result of the Split() function to a variable called output. The output variable now holds an array of each of the words in the string.

go