当前位置:网站首页>Go语言自学系列 | golang嵌套结构体
Go语言自学系列 | golang嵌套结构体
2022-04-23 08:22:00 【COCOgsta】
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:Go语言自学系列 | 汇总_COCOgsta的博客-CSDN博客
go语言没有面向对象编程思想,也没有继承关系,但是可以通过结构体嵌套来实现这种效果。
下面通过实例演示如何实现结构体嵌套,假如有一个人Person结构体,这个人还养了一个宠物Dog结构体
下面我们来看一下:
Dog结构体
type Dog struct {
name string
color string
age int
}
Person结构体
type person struct {
dog Dog
name string
age int
}
访问它们
package main
import "fmt"
type Dog struct {
name string
color string
age int
}
type person struct {
dog Dog
name string
age int
}
func main() {
var tom person
tom.dog.name = "花花"
tom.dog.color = "黑白花"
tom.dog.age = 2
tom.name = "tom"
tom.age = 20
fmt.Printf("tom: %v\n", tom)
}
运行结果
[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
tom: {
{花花 黑白花 2} tom 20}
版权声明
本文为[COCOgsta]所创,转载请带上原文链接,感谢
https://blog.csdn.net/guolianggsta/article/details/124355606
边栏推荐
- Type anonyme (Principes fondamentaux du Guide c)
- 项目上传部分
- 让地球少些“碳”息 度能在路上
- Asan minimalism
- ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
- colorui 解决底部导航遮挡内容问题
- Online app resource download website source code
- 洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
- 对OutputStream类的flush()方法的误解
- Kubernetes如何使用harbor拉去私有镜像
猜你喜欢
随机推荐
DOM学习笔记---遍历页面所有元素节点
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel
关于数组复制问题
Detailed description of self feeling of auricular point weight loss 0422
LINQ学习系列-----1.4 匿名对象
tsdf +mvs
分布式消息中间件框架选型-数字化架构设计(7)
Let the earth have less "carbon" and rest on the road
Ear acupoint diagnosis and treatment essay 0421
okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
Search the complete navigation program source code
洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
5.6 comprehensive case - RTU-
Notes on English class (4)
Shell脚本进阶
线程的调度(优先级)
Queue (C language / linked list)
JVM工具之Arthas使用
Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board