当前位置:网站首页>Go语言自学系列 | golang结构体
Go语言自学系列 | golang结构体
2022-04-21 20:34:00 【COCOgsta】
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:Go语言自学系列 | 汇总_COCOgsta的博客-CSDN博客
go语言没有面向对象的概念了,但是可以使用结构体来实现,面向对象编程的一些特性,例如:继承、组合等特性。
go语言结构体的定义
上一节我们介绍了类型定义,结构体的定义和类型定义类似,只不过多了一个struct关键字,语法结构如下:
type struct_variable_type struct {
member definition;
member definition;
...
member definition;
}
type:结构体定义关键字
struct_variable_type:结构体类型名称
struct:结构体定义关键字
member definition:成员定义
实例
下面我们定义一个人的结构体Person
type Person struct {
id int
name string
age int
email string
}
以上我们定义一个Person结构体,有四个成员,来描述一个Person的信息。
同类型的可以合并到一行,例如:
type Person struct {
id, age int
name, email string
}
声明一个结构体变量
声明一个结构体变量和声明一个普通变量相同,例如:
package main
import "fmt"
type Person struct {
id, age int
name, email string
}
func main() {
var tom Person
fmt.Printf("tom: %v\n", tom)
kite := Person{}
fmt.Printf("kite: %v\n", kite)
}
运行结果
[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
tom: {0 0 }
kite: {0 0 }
结构体成员,在没有赋值之前都是零值。
访问结构体成员
可以使用点运算符(.),来访问结构体成员,例如:
package main
import "fmt"
func main() {
type Person struct {
id, age int
name, email string
}
var tom Person
tom.id = 1
tom.name = "tom"
tom.age = 20
tom.email = "[email protected]"
fmt.Printf("tom: %v\n", tom)
}
运行结果如下:
[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
tom: {1 20 tom [email protected]}
匿名结构体
如果结构体是临时使用,可以不用起名字,直接使用,例如:
package main
import "fmt"
func main() {
var dog struct {
id int
name string
}
dog.id = 1
dog.name = "花花"
fmt.Printf("dog: %v\n", dog)
}
版权声明
本文为[COCOgsta]所创,转载请带上原文链接,感谢
https://blog.csdn.net/guolianggsta/article/details/124325257
边栏推荐
- 在两个TIA博途项目中组态PROFIBUS和PROFINET通信的具体方法
- Construction of distributed second kill system
- 人机验证reCAPTCHA v3使用完备说明
- C# 双保险进程监视器 lol 保证被监视的程序'几乎'永远运行. 关键字:进程操作 进程查看 创建进程
- MySQL view (detailed explanation)
- Data preprocessing for data analysis
- 在IE和Edge中用JS判断只能输入数字,字母,日期型。
- Rhcsa (day 5)
- Andorid -- why use transactions? What is transaction commit and rollback?
- 【高并发】不得不说的线程池与ThreadPoolExecutor类浅析
猜你喜欢

Why do you have no idea when doing data analysis?

Actual combat | e-commerce business performance test (II): JMeter parameterization function realizes data-driven registration and login

RTMP(3):Protocol Control Message

实战 | 电商业务性能测试(二): Jmeter 参数化功能实现注册登录的数据驱动

实战 | JMeter 典型电商场景(下单/支付)的性能压测

getchar,putchar,EOF

After learning this tutorial of capturing packages by Charles, I unloaded Fiddler directly

Pfsense configuring IPSec site to site tunneling using certificate authentication Guide

Changan dark blue c385 product information exposure aims at 200000 level, and the number one target is model 3!

3D 沙盒游戏之人物的点击行走移动
随机推荐
MySQL集群解决方案
RTMP(4):User Control Message
双链表插入、删除操作单步解析(十四)
Know that Chuangyu issued a heavy strategic plan to build a practical defense system for continuous exchange of fire
1075 pat judge (25 points)
Vtkjs introduction
实战 | UI 自动化测试框架设计与 PageObject 改造
Employment of college students in the "most difficult employment season": more than half of the graduates have landed, and higher vocational colleges produce sweet pastries
如何用Sonic云真机打王者
Actual combat | UI automation test framework design and pageobject transformation
Practice of spark SQL in snowball
C# 双保险进程监视器 lol 保证被监视的程序'几乎'永远运行. 关键字:进程操作 进程查看 创建进程
Mysql坑爹指南_sql_mode=“ONLY_FULL_GROUP_BY“所导致错误以及不能使用group by
Actual combat | complete the performance pressure test of typical e-commerce scenarios (home page browsing) based on JMeter
In the morning, I met Tencent and took out 38K, which let me see the basic ceiling
TCP example of grpc implemented by golang
Click, walk and move of characters in 3D sandbox game
The whole process of callback registration and callback of openharmony sensor module
Win11高效日历推荐
VS2019配置opencv4