Arrays and Slices

Let's learn about Go's arrays and slices

Arrays

  • In Go, when creating an array, you must specify the length of that array

    • ex)

      package main
      
      import "fmt"
      
      func main() {
       names := [5]string{"chloe", "camila", "bella"}
       names[3] = "charlotte"
       names[4] = "bree"
       fmt.Print(names)
      }
      • Go's index starts from 0, so in the above example, names[4] is the 5th index

    • If you put an index larger than the specified length when creating an array, an error will occur

Slices

  • Sometimes you want to limit the size of an array, but sometimes you want to add elements without size limitation

    • The data type you can use in such cases is slice

  • Slice in Go is an array but without length

    • ex)

      • The only difference from array is that there's no length

append()

  • When adding items to a slice, use the append() function

  • append() takes two arguments:

    1. slice

    2. value

  • However, append() does not modify the slice

    • append() returns a slice with new values added!

      • So you need to update the slice again

      • ex)

I think I'll probably use slice most of the time!

Last updated