type Human struct{
Name string
Age int
}
func (this Human) setName(name string){
this.Name = name
fmt.Println(this)
}
func (this *Human) setAge(age int){
this.Age = age
fmt.Println(this)
}
func main(){
var human Human
human.setName("tony")
human.setAge(27)
}
打印结果
{tony 0}
&{ 27}
**********************************************************
type Human struct{
Name string
Age int
}
func (this *Human) setName(name string){
this.Name = name
fmt.Println(this)
}
func (this Human) setAge(age int){
this.Age = age
fmt.Println(this)
}
func main(){
var human Human
human.setName("tony")
human.setAge(27)
}
打印结果
&{tony 0}
{tony 27}
值方法并不会改变调用对象的值
指针方法会改变调用方法的值
go 值方法和指针方法
最新推荐文章于 2023-02-18 16:34:41 发布