From 110aab4934d1622751347caf33c2e0c1c231bdbd Mon Sep 17 00:00:00 2001 From: sebastian Date: Wed, 15 Nov 2023 14:18:47 +0100 Subject: [PATCH] Section10-Finished --- Section10/84.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ Section10/87.go | 32 +++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Section10/84.go create mode 100644 Section10/87.go diff --git a/Section10/84.go b/Section10/84.go new file mode 100644 index 0000000..8bca2d8 --- /dev/null +++ b/Section10/84.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "unsafe" +) + +func main() { + // s1 := []int{10, 20, 30, 40, 50} + // s3, s4 := s1[0:2], s1[1:3] + + // s3[1] = 600 + + // fmt.Println(s1) + // fmt.Println(s4) + + // arr1 := [4]int{10, 20, 30, 40} + // slice1, slice2 := arr1[0:2], arr1[1:3] + + // arr1[1] = 2 + + // fmt.Println(slice1) + // fmt.Println(slice2) + // fmt.Println(arr1) + + cars := []string{"Ford", "Honda", "Audi", "Range Rover"} + newCars := []string{} + + newCars = append(newCars, cars[0:2]...) + + cars[0] = "Nissan" + fmt.Println(cars, newCars) + + s1 := []int{10, 20, 30, 40, 50} + newSlice := s1[0:3] + fmt.Println(len(newSlice), cap(newSlice)) + + newSlice = s1[2:5] + fmt.Println(len(newSlice), cap(newSlice)) + + fmt.Printf("%p\n", &s1) + + fmt.Printf("%p %p \n", &s1, &newSlice) + + newSlice[0] = 1000 + fmt.Println("s1: ", s1) + + a := [5]int{1, 2, 3, 4, 5} + s := []int{1, 2, 3, 4, 5} + + fmt.Printf("array's size in bytes: %d \n", unsafe.Sizeof(a)) + fmt.Printf("slice's size in bytes: %d \n", unsafe.Sizeof(s)) + +} diff --git a/Section10/87.go b/Section10/87.go new file mode 100644 index 0000000..eed9224 --- /dev/null +++ b/Section10/87.go @@ -0,0 +1,32 @@ +package main + +import "fmt" + +func main() { + var nums []int + fmt.Printf("%#v\n", nums) + + fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums)) + + nums = append(nums, 1, 2) + fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums)) + + nums = append(nums, 3) + fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums)) + + nums = append(nums, 4) + fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums)) + + nums = append(nums, 5) + fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums)) + + nums = append(nums[0:4], 200, 300, 400, 500, 600) + fmt.Printf("Lenght: %d, Capacity: %d \n", len(nums), cap(nums)) + fmt.Println(nums) + + letters := []string{"A", "B", "C", "D", "E", "F"} + letters = append(letters[:1], "X", "Y") + fmt.Println(letters, len(letters), cap(letters)) + fmt.Println(letters[3:6]) + +}