OneBite.Dev - Coding blog in a bite size

split a string by comma sign in Go

Code snippet on how to split a string by comma sign in Go

  str := "Hello,World"
  strSplit := strings.Split(str,",")
  fmt.Println(strSplit)

This code splits a given string, which is “Hello,World” in this example, into two separate parts, separated by a comma, using the strings.Split() function. First, a string variable, str is declared and assigned the value “Hello,World”. Then, another variable, strSplit, is declared and assigned the result of a call to strings.Split() which takes two parameters- the first is the string to be split and the second is the character that will be used to split the string: in this case a comma. Finally, the resulting array of two strings is printed on the console.

go