Lists

    The major difference between an and a is that with thearray you need to know the size up front. In Go, there is no way toequally easily add values to an existing so if you want toeasily add values, you can initialize a slice at a max length andincrementally add things to it.

    Go

    1. package main
    2.  
    3. import "fmt"
    4.  
    5. func main() {
    6. // initialized array
    7. var numbers [5]int // becomes [0, 0, 0, 0, 0]
    8. numbers[2] = 100
    9. // create a new slice from an array
    10. some_numbers := numbers[1:3]
    11. fmt.Println(some_numbers) // [0, 100]
    12. // length of it
    13. fmt.Println(len(numbers))
    14.  
    15. // initialize a slice
    16. var scores []float64
    17. scores[0] = 2.2 // change your mind
    18. fmt.Println(scores) // prints [2.2]
    19.  
    20. // when you don't know for sure how much you're going
    21. // to put in it, one way is to
    22. var things [100]string
    23. things[0] = "Peter"
    24. things[1] = "Anders"
    25. fmt.Println(len(things)) // 100