当前位置:网站首页>Go语言自学系列 | golang结构体的初始化
Go语言自学系列 | golang结构体的初始化
2022-04-23 08:22:00 【COCOgsta】
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:Go语言自学系列 | 汇总_COCOgsta的博客-CSDN博客
未初始化的结构体,成员都是零值 int 0 flow 0.0 bool false string nil nil
实例
package main
import "fmt"
func main() {
type Person struct {
id, age int
name, email string
}
var tom Person
fmt.Printf("tom: %v\n", tom)
}
运行结果
[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
tom: {0 0 }
使用键值对对结构体进行初始化
实例
package main
import "fmt"
func main() {
type Person struct {
id, age int
name, email string
}
kite := Person{
id: 1,
name: "kite",
age: 20,
email: "[email protected]",
}
fmt.Printf("kite: %v\n", kite)
}
运行结果
[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
kite: {1 20 kite [email protected]}
使用值的列表初始化
实例
package main
import "fmt"
func main() {
type Person struct {
id, age int
name, email string
}
kite := Person{
1,
20,
"kite",
"[email protected]",
}
fmt.Printf("kite: %v\n", kite)
}
运行结果
[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
kite: {1 20 kite [email protected]}
注意:
- 必须初始化结构体的所有字段。
- 初始值的填充顺序必须与字段在结构体中的声明顺序一致。
- 该方式不能和键值初始化方式混用。
部分成员初始化
用不到的成员,可以不进行初始化
package main
import "fmt"
func main() {
type Person struct {
id, age int
name, email string
}
kite := Person{
id: 1,
name: "kite",
}
fmt.Printf("kite: %v\n", kite)
}
运行结果
[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
kite: {1 0 kite }
版权声明
本文为[COCOgsta]所创,转载请带上原文链接,感谢
https://blog.csdn.net/guolianggsta/article/details/124355554
边栏推荐
- 信息收集相关知识点及题解
- Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
- STM32 uses Hal library. The overall structure and function principle are introduced
- 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
- 根据字节码获取类的绝对路径
- pgsql想实现mysql一样样的列子查询操作
- Reference passing 1
- 【深度好文】Flink SQL流批⼀体化技术详解(一)
- How to generate assembly file
- Harbor企业级镜像管理系统实战
猜你喜欢

Excle plus watermark

excle加水印

CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association

5.6 comprehensive case - RTU-

RCC introduction of Hal Library

【路科V0】验证环境2——验证环境组件

How to generate assembly file

《深度学习》学习笔记(八)

Redis Desktop Manager for Mac(Redis可视化工具)

freertos学习02-队列 stream buffer message buffer
随机推荐
洋桃电子STM32物联网入门30步笔记一、HAL库和标准库的区别
分组背包呀
Sword finger offer Day24 math (medium)
根据字节码获取类的绝对路径
记录:js删除数组中某一项或几项的几种方法
RPC过程
分布式消息中间件框架选型-数字化架构设计(7)
Data deletion and modification (MySQL)
swagger文档导出自定义v2/api-docs拦截
LINQ Learning Series ----- 1.4 anonymous objects
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
PgSQL wants to implement all kinds of column sub query operations of MySQL
ATSS(CVPR2020)
How much inventory recording does the intelligent system of external call system of okcc call center need?
《深度学习》学习笔记(八)
'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。
Asan minimalism
Anonymous type (c Guide Basics)
LINQ学习系列-----1.4 匿名对象
QT reading and writing XML files