structs _ golang

Go's structs are typed collections of fields. They're useful for grouping data together to form records

package main

import (
    "fmt"
)

type person struct {
    name string
    age  int
}

func main() {

    fmt.Println(person{"Bob", 20})
    fmt.Println(person{name: "Alice", age: 30})
    fmt.Println(person{name: "Fred"})

    fmt.Println(&person{name: "Ann", age: 40})

    s := person{name: "Sean", age: 50}
    fmt.Println(s.name)

    sp := &s

    fmt.Println(sp.age)

    sp.age = 51

    fmt.Println(sp.age)

}
{Bob 20}
{Alice 30}
{Fred 0}
&{Ann 40}
Sean
50

总结 :

  1 : 通过 & 能得出 struct 的指针

    2 :  You can also use dots with struct pointers - the pointers are automatically dereferenced

原文地址:https://www.cnblogs.com/jackkiexu/p/4337927.html