go的复合类型
package main import "fmt" func main() { arr := [10]int{} //数组 arr[1] = 8 fmt.Println(arr) arr2 := [2][3]string{{"1", "2", "3"}} //二维数组 arr2[1][0] = "4" arr2[1][1] = "5" arr2[1][2] = "5" fmt.Println(arr2) s := []string{"你好", "再见"} //切片(代表内存地址,地址传递) s = append(s, "祝福") sc := []string{"1"} copy(sc, s) //copy(s, sc) fmt.Println(s) fmt.Println(sc) s2 := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8}, {9}} //二维切片 s2 = append(s2, []int{10}) fmt.Println(s2) m := map[int]string{1: "2"} //字典(无序、代表内存地址,地址传递) m[0] = "4" delete(m, 1) fmt.Println(m) }
函数调用:
func main() { a := 1 aa(a) fmt.Println(a) fmt.Println("--------------") var arr [2]int = [2]int{1, 2} arrFunction(arr) fmt.Println(arr) fmt.Println("--------------") slicp := []int{1, 2, 3} slicpFunction(slicp) fmt.Println(slicp) fmt.Println("--------------") m := map[int][]string{} m[0] = []string{"1", "2", "3"} fmt.Println(m) mapFunction(m) fmt.Println(m) fmt.Println("--------------") } func aa(a int) { a += 1 fmt.Println(a) } func arrFunction(a [2]int) { for key, value := range a { a[key] = value + 1 } fmt.Println(a) } func slicpFunction(a []int) { for key, value := range a { a[key] = value + 1 } fmt.Println(a) } func mapFunction(a map[int][]string) { for i := 0; i < len(a); i++ { for key, value := range a[i] { a[i][key] = value + "88" } } }
结果:
结构体复合类型的特性和基础的类型特性一样。
单独结构体传参只是形参传递,并不是地址传递。