加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang语法总结(二十):struct组合

发布时间:2020-12-16 18:33:58 所属栏目:大数据 来源:网络整理
导读:Go的struct可以嵌入组合其它struct,有点像是继承,下面是示例: type human struct { Sex int } type person human //嵌入结构,只需要写出结构类型 Name string Age func main () a := person Name : "cpwl" , : 21 human human //默认用嵌入结构类型名作为
Go的struct可以嵌入组合其它struct,有点像是继承,下面是示例:
   
   
type human struct { Sex int}type person human //嵌入结构,只需要写出结构类型 Namestring Agefunc main() a := person Name:"cpwl",: 21 human human //默认用嵌入结构类型名作为字段名1},76)"> fmt.Println(a)//{{1} cpwl 21}//默认把嵌入结构的字段都给了外层结构,可以直接访问与赋值.Sex//1//保留用嵌入结构类型名作为字段名的方式,//是为了防止多个嵌入结构有相同字段名,发生冲突.human//1}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读