当前位置:网站首页>Go language self-study series | initialization of golang structure
Go language self-study series | initialization of golang structure
2022-04-23 08:50:00 【COCOgsta】
Video source :B standing 《golang Introduction to project practice [2021 newest Go Language course , There is no nonsense , Dry only ! Ongoing update ...]》
Organize the teacher's course content and test notes while studying , And share it with you , Infringement is deleted , Thank you for your support !
Attach summary sticker :Go Language self-study series | Summary _COCOgsta The blog of -CSDN Blog
Uninitialized structure , Members are all zero values int 0 flow 0.0 bool false string nil nil
example
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 results
[Running] go run "d:\SynologyDrive\ software development \go\golang Introduction to project practice \goproject\360duote.com\pro01\test.go"
tom: {0 0 }
Initialize the structure with key value pairs
example
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 results
[Running] go run "d:\SynologyDrive\ software development \go\golang Introduction to project practice \goproject\360duote.com\pro01\test.go"
kite: {1 20 kite [email protected]}
Initialize with a list of values
example
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 results
[Running] go run "d:\SynologyDrive\ software development \go\golang Introduction to project practice \goproject\360duote.com\pro01\test.go"
kite: {1 20 kite [email protected]}
Be careful :
- All fields of the structure must be initialized .
- The initial values must be filled in the same order as the fields are declared in the structure .
- This method cannot be mixed with the key initialization method .
Initialization of some members
Unused members , It can be done without initialization
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 results
[Running] go run "d:\SynologyDrive\ software development \go\golang Introduction to project practice \goproject\360duote.com\pro01\test.go"
kite: {1 0 kite }
版权声明
本文为[COCOgsta]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230822135909.html
边栏推荐
猜你喜欢
论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
STM32 uses Hal library. The overall structure and function principle are introduced
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
L2-024 tribe (25 points) (and check the collection)
2021李宏毅机器学习之Adaptive Learning Rate
Idea import commons-logging-1.2 Jar package
cadence的工艺角仿真、蒙特卡洛仿真、PSRR
L2-3 romantic silhouette (25 points)
RCC introduction of Hal Library
随机推荐
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
Share the office and improve the settled experience
valgrind和kcachegrind使用运行分析
测试你的机器学习流水线
L2-3 浪漫侧影 (25 分)
【原创】使用System.Text.Json对Json字符串进行格式化
Bk3633 specification
php基于哈希算法出现的强弱比较漏洞
Get the absolute path of the class according to the bytecode
L2-023 graph coloring problem (25 points) (graph traversal)
Valgrind and kcache grind use run analysis
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
Wechat: get the owner of a single tag
Illegal character in scheme name at index 0:
OneFlow学习笔记:从Functor到OpExprInterpreter
Idea import commons-logging-1.2 Jar package
Idea package jar file
valgrind和kcachegrind使用運行分析
Study notes of deep learning (8)
求简单类型的矩阵和