golang指针使用
package main
import (
"fmt"
)
type Cat struct {
age int
}
func (cat Cat) Age() int {
return cat.age
}
func (cat Cat) SetAge(x int) {
cat.age = x
}
type Dog struct {
age int
}
func (dog Dog) Age() int {
return dog.age
}
func (dog *Dog) SetAge(x int) {
dog.age = x
}
func main() {
cat := new(Cat)
cat.SetAge(5)
fmt.Printf("%d\n", cat.Age())
dog := new(Dog)
dog.SetAge(5)
fmt.Printf("%d\n", dog.Age())
}