指针结构体

编程

https://juejin.im/post/5ca2f37ce51d4502a27f0539

  • type Member struct {

    id int

    name, email string

    gender, age int

    }

    func TestChange(t *testing.T) {

    m1 := Member{}

    m2 := new(Member)

    Change(m1, m2) //值传递,这里需要传指针才有效

    fmt.Println(m1, m2) //{0 0 0} &{0 小红 0 0}

    }

    func Change(m1 Member, m2 *Member) {

    m1.name = "小明"

    m2.name = "小红"

    }

    func TestMember(t *testing.T) {

    var m1 Member //所有字段均为空值

    var m2 = &Member{}

    var m3 = new(Member)

    var m4 = &m1

    m1.age = 12

    m2.age = 13

    m3.age = 14

    m4.age = 145

    t.Logf("%T %v", m1, m1.age)

    t.Logf("%T %v", m2, m2.age)

    t.Logf("%T %v", m3, m3.age)

    t.Logf("%T %v", m4, m4.age)

    }

 

 

以上是 指针结构体 的全部内容, 来源链接: utcz.com/z/517714.html

回到顶部