Copy in Go

Copy slice to another slice: 1 2 3 4 5 6 7 8 evens := []int{2, 4} odds := []int{3, 5, 7} N := copy(evens, odds) fmt.Printf("%d element(s) are copied.\n", N) // evens after copy is, [3, 5] // odds is intact after copy, [3, 5, 7] copy can be used over append if want to keep length of target slice intact.

Full slice expression

Simple slice expression: “[low:high]” 1 2 3 4 5 numbers := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} s := numbers[1:4] fmt.Println(s) // [1, 2, 3] fmt.Println(len(s)) // len = 3 fmt.Println(cap(s)) // cap = 9 Full slice expression:

Go errors

non-constant array bound max Array length is not a constant. slice can only be compared to nil Invalid comparison between slices. Slice can only be compared to nil.

Slice append function in Go

Append an element to the slice: 1 2 nums := []int{1, 2, 3} nums = append(nums, 4) Append multiple elements to the slice: 1 2 nums := []int{1, 2} nums = append(nums, 3, 4) Append slice to another slice:

Keyed array elements in Go

Unkeyed array elements: 1 2 3 4 5 6 7 example := [3]float64{ 1.3, 6.7, 4.0, } Keyed array elements: 1 2 3 4 5 6 7 example := [3]float64{ 0: 1.3, 1: 6.7, 2: 4.0, } Keyed array in different order: