OneBite.Dev - Coding blog in a bite size

convert an array to a string in Go

Code snippet on how to convert an array to a string in Go

  s := []string{"a", "b", "c"}
  result := strings.Join(s, "")
  fmt.Println(result)

This code converts an array of strings to a single string in Go. The first line declares a variable, “s”, to be an array of strings. The next line creates a variable, “result”, that combines the strings in the array using the strings.Join() function. This function takes as its arguments the array of strings to be joined and the character to be used to separate them. In this case, an empty string is used so that the elements are joined together without any separation. The last line prints out the variable “result”, which should now contain the single string created from the array.

go