|
@@ -0,0 +1,66 @@
|
|
|
|
+package main
|
|
|
|
+
|
|
|
|
+import "fmt"
|
|
|
|
+
|
|
|
|
+func add1(s []int, x int) []int {
|
|
|
|
+ s = append(s, x)
|
|
|
|
+ fmt.Printf("add1-addrness:%p", &s)
|
|
|
|
+ fmt.Println("")
|
|
|
|
+ return s
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+func add2(s []int, x int) {
|
|
|
|
+ s = append(s, x)
|
|
|
|
+}
|
|
|
|
+func add3(s [10]int, x int) {
|
|
|
|
+ s[9] = 10
|
|
|
|
+ fmt.Println(s)
|
|
|
|
+ fmt.Printf("add1-addrness:%p", &s)
|
|
|
|
+ fmt.Println("")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func main() {
|
|
|
|
+ // sliceList()
|
|
|
|
+ // nums := make([]int, 1)
|
|
|
|
+ // nums = append(nums, 0)
|
|
|
|
+ // fmt.Println(nums, len(nums), cap(nums))
|
|
|
|
+ sliceAppend()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func sliceList() {
|
|
|
|
+ //切片,引用类型
|
|
|
|
+ a := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
|
|
+ fmt.Println(a)
|
|
|
|
+ s1 := a[5:8] //len=3 cap=5
|
|
|
|
+ fmt.Println("s1", s1)
|
|
|
|
+ fmt.Printf("s1-addrness:%p", &s1)
|
|
|
|
+ fmt.Println("")
|
|
|
|
+ add1(s1, 0)
|
|
|
|
+ fmt.Println("a", a)
|
|
|
|
+ fmt.Println("s1", s1)
|
|
|
|
+
|
|
|
|
+ add2(s1, 1)
|
|
|
|
+ fmt.Println("a", a)
|
|
|
|
+ fmt.Println("s1", s1)
|
|
|
|
+ //数组,值类型
|
|
|
|
+ fmt.Println("值类型-------------------")
|
|
|
|
+ b := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
|
|
+ fmt.Printf("b的addrness:%p\n", &b)
|
|
|
|
+ add3(b, 0)
|
|
|
|
+ fmt.Println(b)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func sliceAppend() {
|
|
|
|
+ arr := [5]int{1, 2, 3, 4} // [1 2 3 4 0]
|
|
|
|
+ fmt.Println(arr)
|
|
|
|
+
|
|
|
|
+ s2 := arr[2:] // [3 4 0]
|
|
|
|
+ printSlice(s2)
|
|
|
|
+ s2 = append(s2, 5)
|
|
|
|
+ printSlice(s2)
|
|
|
|
+ fmt.Println(arr)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func printSlice(s []int) {
|
|
|
|
+ fmt.Printf("len=%d cap=%d %p %v\n", len(s), cap(s), s, s)
|
|
|
|
+}
|