Go-struct

struct(结构体)

struct 类型由多个不同类型元素组合而成。struct 存储空间是连续,它按照字段声明时的顺序存储

struct 形式

  • struct 类型字面量

    struct {
        FeildName FeildType
        FeildName FeildType
        FeildName FeildType
    }
    
  • 使用type 关键字声明的自定义struct类型

    type TypeName struct {
        FeildName FeildType
        FeildName FeildType
        FeildName FeildType
    }
    

初始化

type Person struct {
    Name string
    Age int
}

type Student struct {
    *Person
    Number int
}

p := &Person{
    Name:"biao",
    Age: 18,
}

s := Student {
    Person: p,
    Number: 100,
}
posted @ 2021-03-08 21:23  KuBee  阅读(78)  评论(0编辑  收藏  举报