package main import ( "fmt" "reflect" ) type User struct { Name string Age int } func copyPoint(m *User) *User{ vt := reflect.TypeOf(m).Elem() fmt.Println(vt) newoby := reflect.New(vt) newoby.Elem().Set(reflect.ValueOf(m).Elem()) return newoby.Interface().(*User) } func main(){ user := &User{} user.Name = "sunlong" user.Age = 32 fmt.Println(user) fmt.Printf("%p ",user) user2 := copyPoint(user) fmt.Printf("%p ",user) fmt.Printf("%p ",user2) user.Name="lisi" fmt.Printf("%v ",user) fmt.Printf("%v ",user2) return //user2 := *user //fmt.Printf("%v ",user) //fmt.Printf("%v ",user2) //user2.Age = 33 //user2.Name = "lisi" //fmt.Println(*user) //fmt.Println(user2) }
&{sunlong 32} 0xc000004480 main.User 0xc000004480 0xc0000044e0 &{lisi 32} &{sunlong 32}