当前位置:网站首页>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
边栏推荐
- 虚拟线上展会-线上vr展馆实现24h沉浸式看展
- LINQ学习系列-----1.4 匿名对象
- Protobuf简介
- DOM学习笔记---遍历页面所有元素节点
- Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
- Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi
- 作文以记之 ~ 二叉树的后序遍历
- Talk about the basic but not simple stock data
- How browser works
- Goland 调试go使用-大白记录
猜你喜欢

Goland 调试go使用-大白记录

Flink SQL实现流批一体

vmware 搭建ES8的常见错误

00后最关注的职业:公务员排第二,第一是?

QT reading and writing XML files

Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code

Shell script advanced

Harbor企业级镜像管理系统实战

idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)

跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
随机推荐
Record: JS several methods to delete one or more items in the array
Notes on English class (4)
四张图弄懂matplotlib的一些基本用法
什么是RPC
word加水印
Introduction to protobuf
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
Add random attributes to the Li class array objects and sort them
项目上传部分
synchronized 锁的基本用法
线程的调度(优先级)
匿名类型(C# 指南 基础知识)
5.6 comprehensive case - RTU-
LINQ学习系列-----1.4 匿名对象
正点原子携手OneOS直播 OneOS系统教程全面上线
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
stm32以及freertos 堆栈解析
lgb,xgb,cat k折交叉验证
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction