struct 真的是copy value , 要用 pointer 才能指到同一個 struct variable
package main
import "fmt"
type myst struct {
age int
}
var thest myst = myst{age: 1}
func getstp() *myst {
return &thest
}
func getst() myst {
return thest
}
func main() {
for i := 0; i < 10; i++ {
st := getst()
fmt.Println(st)
st.age--
}
fmt.Println("with pointer")
for i := 0; i < 10; i++ {
stp := getstp()
fmt.Println(*stp)
stp.age--
}
}
map 不必特別用 pointer, 這種collection type, assignment其實都是 change reference