diff --git a/eBook/examples/chapter_7/array_literals.go b/eBook/examples/chapter_7/array_literals.go new file mode 100644 index 0000000..5b9ad34 --- /dev/null +++ b/eBook/examples/chapter_7/array_literals.go @@ -0,0 +1,14 @@ +package main +import "fmt" + +func main() { + // var arrAge = [5]int{18, 20, 15, 22, 16} + // var arrLazy = [...]int{5, 6, 7, 8, 22} + // var arrLazy = []int{5, 6, 7, 8, 22} + var arrKeyValue = [5]string{3: "Chris", 4: "Ron"} + // var arrKeyValue = []string{3: "Chris", 4: "Ron"} + + for i:=0; i < len(arrKeyValue); i++ { + fmt.Printf("Person at %d is %s\n", i, arrKeyValue[i]) + } +} diff --git a/eBook/examples/chapter_7/array_slices.go b/eBook/examples/chapter_7/array_slices.go new file mode 100644 index 0000000..de3ffdc --- /dev/null +++ b/eBook/examples/chapter_7/array_slices.go @@ -0,0 +1,32 @@ +package main +import "fmt" + +func main() { + var arr1 [6]int + var slice1 []int = arr1[2:5] // item at index 5 not included! + + // load the array with integers: 0,1,2,3,4,5 + for i := 0; i < len(arr1); i++ { + arr1[i] = i + } + + // print the slice + for i := 0; i < len(slice1); i++ { + fmt.Printf("Slice at %d is %d\n", i, slice1[i]) + } + + fmt.Printf("The length of arr1 is %d\n", len(arr1)) + fmt.Printf("The length of slice1 is %d\n", len(slice1)) + fmt.Printf("The capacity of slice1 is %d\n", cap(slice1)) + +// grow the slice + slice1 = slice1[0:4] + for i := 0; i < len(slice1); i++ { + fmt.Printf("Slice at %d is %d\n", i, slice1[i]) + } + fmt.Printf("The length of slice1 is %d\n", len(slice1)) + fmt.Printf("The capacity of slice1 is %d\n", cap(slice1)) + +// grow the slice beyond capacity + //slice1 = slice1[0:7 ] // panic: runtime error: slice bound out of range +} diff --git a/eBook/examples/chapter_7/array_sum.go b/eBook/examples/chapter_7/array_sum.go new file mode 100644 index 0000000..a1a3a7f --- /dev/null +++ b/eBook/examples/chapter_7/array_sum.go @@ -0,0 +1,16 @@ +package main +import "fmt" + +func main() { + array := [3]float64{7.0, 8.5, 9.1} + x := Sum(&array) // Note the explicit address-of operator + // to pass a pointer to the array + fmt.Printf("The sum of the array is: %f", x) +} + +func Sum(a *[3]float64) (sum float64) { + for _, v := range a { // derefencing *a to get back to the array is not necessary! + sum += v + } + return +} diff --git a/eBook/examples/chapter_7/copy_append_slice.go b/eBook/examples/chapter_7/copy_append_slice.go new file mode 100644 index 0000000..a20a291 --- /dev/null +++ b/eBook/examples/chapter_7/copy_append_slice.go @@ -0,0 +1,15 @@ +package main +import "fmt" + +func main() { + sl_from := []int{1, 2, 3} + sl_to := make([]int, 10) + + n := copy(sl_to, sl_from) + fmt.Println(sl_to) + fmt.Printf("Copied %d elements\n", n) // n == 3 + + sl3 := []int{1, 2, 3} + sl3 = append(sl3, 4, 5, 6) + fmt.Println(sl3) +} diff --git a/eBook/examples/chapter_7/make_slice.go b/eBook/examples/chapter_7/make_slice.go new file mode 100644 index 0000000..f7d9bf4 --- /dev/null +++ b/eBook/examples/chapter_7/make_slice.go @@ -0,0 +1,17 @@ +package main +import "fmt" + +func main() { + var slice1 []int = make([]int, 10) + // load the array/slice: + for i := 0; i < len(slice1); i++ { + slice1[i] = 5 * i + } + + // print the slice: + for i := 0; i < len(slice1); i++ { + fmt.Printf("Slice at %d is %d\n", i, slice1[i]) + } + fmt.Printf("\nThe length of slice1 is %d\n", len(slice1)) + fmt.Printf("The capacity of slice1 is %d\n", cap(slice1)) +} diff --git a/eBook/examples/chapter_7/multidim_array.go b/eBook/examples/chapter_7/multidim_array.go new file mode 100644 index 0000000..c787ec2 --- /dev/null +++ b/eBook/examples/chapter_7/multidim_array.go @@ -0,0 +1,16 @@ +package main +const ( + WIDTH = 1920 + HEIGHT = 1080 +) + +type pixel int +var screen [WIDTH][HEIGHT]pixel + +func main() { + for y := 0; y < HEIGHT; y++ { + for x := 0; x < WIDTH; x++ { + screen[x][y] = 0 + } + } +} diff --git a/eBook/examples/chapter_7/pointer_array.go b/eBook/examples/chapter_7/pointer_array.go new file mode 100644 index 0000000..6f5e9a4 --- /dev/null +++ b/eBook/examples/chapter_7/pointer_array.go @@ -0,0 +1,10 @@ +package main +import "fmt" +func f(a [3]int) { fmt.Println(a) } +func fp(a *[3]int) { fmt.Println(a) } + +func main() { + var ar [3]int + f(ar) // passes a copy of ar + fp(&ar) // passes a pointer to ar +} diff --git a/eBook/examples/chapter_7/pointer_array2.go b/eBook/examples/chapter_7/pointer_array2.go new file mode 100644 index 0000000..0096720 --- /dev/null +++ b/eBook/examples/chapter_7/pointer_array2.go @@ -0,0 +1,10 @@ +package main +import "fmt" + +func fp(a *[3]int) { fmt.Println(a) } + +func main() { + for i := 0; i < 3; i++ { + fp(&[3]int{i, i * i, i * i * i}) + } +} diff --git a/eBook/examples/chapter_7/reslicing.go b/eBook/examples/chapter_7/reslicing.go new file mode 100644 index 0000000..87fe45d --- /dev/null +++ b/eBook/examples/chapter_7/reslicing.go @@ -0,0 +1,17 @@ +package main +import "fmt" + +func main() { + slice1 := make([]int, 0, 10) + // load the slice, cap(slice1) is 10: + for i := 0; i < cap(slice1); i++ { + slice1 = slice1[0:i+1] + slice1[i] = i + fmt.Printf("The length of slice is %d\n", len(slice1)) + } + + // print the slice: + for i := 0; i < len(slice1); i++ { + fmt.Printf("Slice at %d is %d\n", i, slice1[i]) + } +} diff --git a/eBook/examples/chapter_7/slices_forrange.go b/eBook/examples/chapter_7/slices_forrange.go new file mode 100644 index 0000000..772a663 --- /dev/null +++ b/eBook/examples/chapter_7/slices_forrange.go @@ -0,0 +1,15 @@ +package main +import "fmt" + +func main() { + slice1 := make([]int, 4) + + slice1[0] = 1 + slice1[0] = 2 + slice1[0] = 3 + slice1[0] = 4 + + for ix, value := range slice1 { + fmt.Printf("Slice at %d is: %d\n", ix, value) + } +}