zoukankan      html  css  js  c++  java
  • golang复制一个指针对象(反射)

    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} 
  • 相关阅读:
    结构型模式(一) 适配器模式
    选择器
    CSS引入
    CSS语法
    CSS介绍
    HTML练习
    HTML标签嵌套规则(重点)
    HTML标签分类(重点)
    HTML标签属性
    body标签
  • 原文地址:https://www.cnblogs.com/sunlong88/p/12902828.html
Copyright © 2011-2022 走看看