diff --git a/chapter-3-composite-types/01_arrays.go b/chapter-3-composite-types/01_arrays.go index 51406f6..900ceb3 100644 --- a/chapter-3-composite-types/01_arrays.go +++ b/chapter-3-composite-types/01_arrays.go @@ -1,6 +1,8 @@ // array literals are their own types // [3]int is a different type to [4]int +// dont use arrays unless you know the exact length ahead of time + package main import "fmt" @@ -17,4 +19,15 @@ func main() { // can leave the size off if initialising an array 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]) } diff --git a/chapter-3-composite-types/02_slices.go b/chapter-3-composite-types/02_slices.go new file mode 100644 index 0000000..374a26c --- /dev/null +++ b/chapter-3-composite-types/02_slices.go @@ -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) +}