How Do You Create a List of Strings in Golang?

Problem scenario
Lists have a fixed length number of discrete items. You want the items in the list to be strings. How do you create a list of strings in Golang?

Solution
1. Use this program called animal.go:

package main

import "fmt"

func main() {
  var x [10]string
  x[0] = "dog"
  x[1] = "cat"
  x[2] = "hamster"
  x[3] = "rabbit"
  x[4] = "chicken"

  for i := 0; i < 5; i++ {
    fmt.Println(x[i])
  }
}

2. Run it like this: go run animal.go

Leave a comment

Your email address will not be published. Required fields are marked *