当前位置:网站首页>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
边栏推荐
- 四张图弄懂matplotlib的一些基本用法
- 队列(c语言/链表)
- ELK生产实践
- jsp页面编码
- 跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
- synchronized 锁的基本用法
- JS中复制数组
- 匿名类型(C# 指南 基础知识)
- Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
- The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
猜你喜欢
洋桃电子STM32物联网入门30步笔记一、HAL库和标准库的区别
HAL库的RCC简介
excle加水印
虚拟线上展会-线上vr展馆实现24h沉浸式看展
【深度好文】Flink SQL流批⼀体化技术详解(一)
Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
QT compilation qtxlsx Library
ATSS(CVPR2020)
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
随机推荐
LeetCode-199-二叉树的右视图
Misunderstanding of flush () method of OutputStream class
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
synchronized 实现原理
Add random attributes to the Li class array objects and sort them
An example of network communication based on TCP / IP protocol -- file transmission
作文以记之 ~ 二叉树的前序遍历
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
一个必看的微信小程序开发指南1-基础知识了解
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
IDEA导入commons-logging-1.2.jar包
CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association
QFileDialog select multiple files or folders
什么是RPC
洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
Generate and parse tokens using JWT
PDF with watermark
四张图弄懂matplotlib的一些基本用法
Ear acupoint diagnosis and treatment essay 0421
对li类数组对象随机添加特性,并进行排序