值传递(GO中只用值传递)
引用传递
- 不显示出现指针,却可以将内存里面唯一一份资源的地址传递
引用类型!=引用传递
引用类型1、map
func makemap(t *maptype, hint int, h *hmap) *hmap {}
引用类型2、chan
func makechan(t *chantype, size int) *hchan {}
引用类型3、slice
func main() {
i:= 19
p:=Person{name: "张三",age:&i}
fmt.Println(p)
modify(p)
fmt.Println(p)
}
type Person struct {
name string
age * int
}
func (p Person) String() string{
return "姓名为:" + p.name + ",年龄为:"+ strconv.Itoa(*p.age)
}
func modify(p Person){
p.name = "李四"
*p.age = 20
}
姓名为:张三,年龄为:19
姓名为:张三,年龄为:20
modify(&p)
func modify(p *Person){
p.name = "李四"
*p.age = 20
}
type slice struct {
array unsafe.Pointer
len int
cap int
}
- 所以,slice是一个struct,普通的值传递,只不过array是指针,可以通过它修改内容
引用类型4(方法和函数)