func (this Person) 和 func (this *Person)的区别
func (this Person)
package main
import (
"fmt"
)
type Person struct {
name string
age int
}
func (this Person) show() {
fmt.Println("name=", this.name, "age=", this.age)
}
func (this Person) setName(name string) {
this.name = name
}
func main() {
alice := Person{"Alice", 19}
alice.show()
fmt.Println("------------")
alice.setName("alice111")
alice.show()
}
VVV
name= Alice age= 19
------------
name= alice111 age= 19
func (this *Person)
package main
import (
"fmt"
)
type Person struct {
name string
age int
}
func (this *Person) show() {
fmt.Println("name=", this.name, "age=", this.age)
}
func (this *Person) setName(name string) {
this.name = name
}
func main() {
alice := Person{"Alice", 19}
alice.show()
fmt.Println("------------")
alice.setName("alice111")
alice.show()
}
VVV
name= Alice age= 19
------------
name= alice111 age= 19