Răsfoiți Sursa

modify 07.1.md

chidouhu 12 ani în urmă
părinte
comite
9dbd1c5d2a

+ 14 - 0
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])
+    }
+}

+ 32 - 0
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
+}

+ 16 - 0
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
+}

+ 15 - 0
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)
+}

+ 17 - 0
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))
+}

+ 16 - 0
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
+        }
+    }
+}

+ 10 - 0
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
+}

+ 10 - 0
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})
+    }
+}

+ 17 - 0
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])
+    }
+}

+ 15 - 0
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)
+    }
+}