OneBite.Dev - Coding blog in a bite size

create an array of string in Go

Code snippet on how to create an array of string in Go

  // Create an array of strings
  animals := []string{"cat", "dog", "turtle"}

This code creates an array of strings called “animals”, containing the strings “cat”, “dog”, and “turtle”. First, the keyword ”:=” is used which declares a new variable (in this case, “animals”) as well as assign a value to it in the same line. Then, using ”[ ]” brackets, the data type of the new variable is specified as an array of strings. Finally, between the brackets, a list of strings to initialize the array is provided. In this example, the array is populated with the strings “cat”, “dog”, and “turtle”.

go