1/6
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What are arrays?
Arrays are used to store multiple values of the same type in a single variable, instead of declaring separate variables for each value.
Write the two ways to declare an array with the var keyword:
var array_name = [length]datatype{values}
var array_name = [...]datatype{values}
Write the two ways to declare an array with := sign:
array_name := [length]datatype{values}
array_name := [...]datatype{values} // here length is inferred
What is the output of this snippet?:
package main
import ("fmt")
func main() {
var arr1 = [3]int{1,2,3}
arr2 := [5]int{4,5,6,7,8}
fmt.Println(arr1)
fmt.Println(arr2)
}
[1 2 3]
[4 5 6 7 8]
What is the output of this snippet?:
package main
import ("fmt")
func main() {
var arr1 = [...]int{1,2,3}
arr2 := [...]int{4,5,6,7,8}
fmt.Println(arr1)
fmt.Println(arr2)
}
[1 2 3]
[4 5 6 7 8]
Write a snippet to print the following output with inferred lengths:[1 2 3]
[4 5 6 7 8]
package main
import ("fmt")
func main() {
var arr1 = [...]int{1,2,3}
arr2 := [...]int{4,5,6,7,8}
fmt.Println(arr1)
fmt.Println(arr2)
}
What is the output of this code:
package main
import ("fmt")
func main() {
arr1 := [5]int{1:10,2:40}
fmt.Println(arr1)
}
[0 10 40 0 0]