2022-12-03

This commit is contained in:
2022-12-03 23:07:58 +00:00
parent 3da7b7dbc7
commit 27f40a2069
2 changed files with 29 additions and 0 deletions

View File

@@ -1,6 +1,8 @@
// array literals are their own types // array literals are their own types
// [3]int is a different type to [4]int // [3]int is a different type to [4]int
// dont use arrays unless you know the exact length ahead of time
package main package main
import "fmt" import "fmt"
@@ -17,4 +19,15 @@ func main() {
// can leave the size off if initialising an array // can leave the size off if initialising an array
var z = [...]int{1, 2, 3} var z = [...]int{1, 2, 3}
// comparison
fmt.Println(z == x)
// an array of length 2, of type array length 3 (2x3 matrix)
var m [2][3]int
fmt.Println(m)
// accessing elements
fmt.Println(x[0])
fmt.Println(x[len(x)-1])
} }

View File

@@ -0,0 +1,16 @@
// the length is not part of the type for a slice
// slices use the same syntax as arrays for accessing/assigning
package main
import "fmt"
func main() {
// []int makes a slice, [...]int makes an array
x := []int{10, 20, 30}
fmt.Println(x)
x = []int{1, 5: 4, 6, 10: 20, 11}
fmt.Println(x)
}