Consider the following Go program. What will it print?
package main import "fmt" func main() { var arr [3]int fmt.Println(len(arr)) fmt.Println(cap(arr)) }
Remember that arrays in Go have fixed size known at compile time.
Arrays in Go have a fixed length. The len function returns the length of the array, which is 3 here. The cap function also returns the capacity, which for arrays is the same as the length.
What error or output results from this Go code?
package main func main() { var a [3]int var b [4]int a = b }
Arrays of different sizes are different types in Go.
In Go, arrays include their size in their type. You cannot assign an array of size 4 to one of size 3.
Identify the problem in this code and what error it causes.
package main import "fmt" func modify(arr [3]int) { arr[0] = 100 } func main() { a := [3]int{1,2,3} modify(a) fmt.Println(a) }
Think about how arrays are passed to functions in Go.
Arrays are passed by value in Go, so the function modifies a copy. The original array remains unchanged.
Choose the best description of a limitation of arrays in Go.
Think about what happens if you want to add more elements than the array size.
Arrays in Go have a fixed size set at compile time and cannot be resized. Slices are used for dynamic sizes.
What does this program print?
package main import "fmt" func main() { a := [3]int{1, 2, 3} b := [3]int{1, 2, 3} c := [3]int{3, 2, 1} fmt.Println(a == b) fmt.Println(a == c) }
Arrays in Go can be compared element-wise if they have the same type and size.
Arrays are equal if all their elements are equal. Here, a and b are equal, a and c are not.