Go 数组(2)
把同样类型的一个数组赋值给另外一个数组
package main; import "fmt"; func main() { var array1[5] string ; array2:=[5]string {"red","blue","write","yellow","fink"} array3:=[4]string {"red","blue","write","yellow"} array1=array2; fmt.Println(array1); fmt.Println(array2) fmt.Println(array3) }
输出:
[red blue write yellow fink]
[red blue write yellow fink]
[red blue write yellow]
数组变量的类型包括数组长度和每个元素的类型。只有这两部分都相同的数组,才是类型相同的数组,才能互相赋值,
不然会报错
把一个指针数组赋值给另一个
package main; import "fmt"; func main() { var array1[3]*string ; fmt.Println(array1); array2 := [3]*string{new(string), new(string), new(string)} *array2[0]="red"; *array2[1]="blue"; *array2[2]="screen" array1 = array2 fmt.Println(array1); fmt.Println(array2) }
输出:
[<nil> <nil> <nil>] [0xc0000501c0 0xc0000501d0 0xc0000501e0] [0xc0000501c0 0xc0000501d0 0xc0000501e0]
越努力越幸运