当前位置:网站首页>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
边栏推荐
猜你喜欢
Harbor企业级镜像管理系统实战
【深度好文】Flink SQL流批⼀体化技术详解(一)
Input / output system
How to generate assembly file
Let the earth have less "carbon" and rest on the road
vmware 搭建ES8的常见错误
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
Excle plus watermark
freertos学习02-队列 stream buffer message buffer
Using qlst excel file
随机推荐
An example of network communication based on TCP / IP protocol -- file transmission
作文以记之 ~ 二叉树的前序遍历
Redis master-slave server problem
excle加水印
微信小程序 catchtap=“toDetail“ 事件问题
第一性原理 思维导图
'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
Knowledge points and problem solutions related to information collection
Search the complete navigation program source code
Detailed description of self feeling of auricular point weight loss 0422
数据可视化:使用Excel制作雷达图
RCC introduction of Hal Library
K210学习笔记(二) K210与STM32进行串口通信
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
396. Rotate Function
okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
Use of applicationreadyevent
Word plus watermark