Section10-Finished

This commit is contained in:
2023-11-15 14:18:47 +01:00
parent f0c2e76a4f
commit 110aab4934
2 changed files with 86 additions and 0 deletions

54
Section10/84.go Normal file
View File

@@ -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))
}

32
Section10/87.go Normal file
View File

@@ -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])
}