package main
import (
"fmt"
"reflect"
)
func main() {
a := [...]string{"Felix", "Anatole", "Juice"}
fmt.Println(len(a)) //3
fmt.Println(reflect.TypeOf(a)) //[3]string
b := [...]string{"Felix", "Anatole", "Juice", "Bin"}
fmt.Println(len(b)) //4
fmt.Println(reflect.TypeOf(b)) //[4]string
fmt.Println(reflect.TypeOf(&b)) //*[4]string
Change(&b)
fmt.Println(b[0]) //Felix2
//Change(&a)// compile error 参数个数异常
s := b[:]
Change2(s)
fmt.Println(b[0]) //Felix Change2,也会更改原数组的值
fmt.Println(reflect.TypeOf(s)) //[]string
Change2(a[:])
fmt.Println(a[0], a[1]) //Felix Change2, Anatole
}
//传指针
func Change(arr *[4]string) {
arr[0] = "Felix2"
}
func Change2(s []string) {
s[0] = "Felix Change2"
}
网友评论