menu arrow_back 湛蓝安全空间 |狂野湛蓝,暴躁每天 chevron_right All_wiki chevron_right the-way-to-go_ZH_CN chevron_right eBook chevron_right 18.4.md
  • home 首页
  • brightness_4 暗黑模式
  • cloud
    xLIYhHS7e34ez7Ma
    cloud
    湛蓝安全
    code
    Github
    18.4.md
    713 B / 2024-07-16 23:14:29
        # 18.4 结构体
    
    - 创建:
    
    ```go
    type struct1 struct {
        field1 type1
        field2 type2
        …
    }
    ms := new(struct1)
    ```
    
    - 初始化:
    
    ```go
    ms := &struct1{10, 15.5, "Chris"}
    ```
    
    当结构体的命名以大写字母开头时,该结构体在包外可见。
    通常情况下,为每个结构体定义一个构建函数,并推荐使用构建函数初始化结构体(参考[例 10.2](examples/chapter_10/person.go)):
    
    
    ```go    
    ms := Newstruct1{10, 15.5, "Chris"}
    func Newstruct1(n int, f float32, name string) *struct1 {
        return &struct1{n, f, name} 
    }
    ```
    
    ## 链接
    
    - [目录](directory.md)
    - 上一节:[映射](18.3.md)
    - 下一节:[接口](18.5.md)
    
    
    links
    file_download