14-arrays/questions/2-arrays.md
gadgets := [...]string{"Mighty Mouse", "Amazing Keyboard", "Shiny Monitor"}
4: Yes! There are 3 elements in the element list. So, Go sets the length of the array to 3.
gadgets := [...]string{}
1: Yes! There are no elements in the element list. So, Go sets the length of the array to 0.
package main
import "fmt"
func main() {
gadgets := [3]string{"Confused Drone"}
fmt.Printf("%q\n", gadgets)
}
1: %q verb doesn't print the type of an array.
2, 4: Array's length cannot change depending on the elements.
3: Yes! Go sets the uninitialized elements to their zero values.
gadgets := [3]string{"Confused Drone"}
gears := [...]string{"Confused Drone"}
fmt.Println(gadgets == gears)
2: Yes! gadget's type is [3]string whereas gears's type is [1]string.
gadgets := [3]string{"Confused Drone", "Broken Phone"}
gears := gadgets
gears[2] = "Shiny Mouse"
fmt.Printf("%q\n", gadgets)
2: Yes! When you assign an array, Go creates a copy of the original array. So, gadgets and gears arrays are not connected. Changing one of them won't effect the other one.
digits := [...][5]string{
{
"## ",
" # ",
" # ",
" # ",
"###",
},
[5]string{
"###",
" #",
"###",
" #",
"###",
},
}
3: Awesome! There are two inner arrays, so the outer array's length becomes 2. Also note that,
[5]stringin front of the second element is unnecessary.
rates := [...]float64{
5: 1.5,
2.5,
0: 0.5,
}
fmt.Printf("%#v\n", rates)
1: That's right! For the explanation check out the example in the course repository here: https://github.com/inancgumus/learngo/tree/master/14-arrays/11-keyed-elements/06-keyed-and-unkeyed
type three [3]int
nums := [3]int{1, 2, 3}
nums2 := three{1, 2, 3}
fmt.Println(nums == nums2)
Note: To solve this question you need to watch the comparison and unnamed types lectures.
1: Yes! They both have the same underlying types: [3]int
type (
threeA [3]int
threeB [3]int
)
nums := threeA{1, 2, 3}
nums2 := threeA(threeB{1, 2, 3})
fmt.Println(nums == nums2)
Note: To solve this question you need to the watch comparison and unnamed types lectures.
1: Yes! Actually, arrays have different types, so normally they're not comparable. However, when you convert
threeB{1, 2, 3}array tothreeAtype, they become comparable.