16-slices/questions/1-slices-vs-arrays.md
2: A slice's length is not a part of its type. So its length can change at runtime.
// Let's say there's a function like this.
func sort(nums []int) {
// ...
}
1: You can't call the sort function using an array. It expects an int slice.
2: You can't call the sort function using an int32 slice. It expects an int slice.
3: That's right! You can pass an int slice to the sort function.
var tasks []string
3: This is a nil slice. Unlike an array, a slice's zero value is nil.
var tasks []string
fmt.Println(len(tasks))
1: Yes, you can use the len function on a nil slice. It returns 0 because the slice doesn't contain any elements yet.
var tasks []string
fmt.Println(tasks[0])
4: You can't get an element that does not exist. A nil slice does not contain any elements.
colors := []string{"red", "blue", "green"}
tones := []string{"dark", "light"}
if colors == tones {
// ...
}
3: That's right! A slice value can only be compared to a nil value.
[]uint64{}
3: That's right. This is an empty slice, it doesn't contain any elements.
[]string{"I'm", "going", "to", "stay", "\"here\""}