当前位置:网站首页>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
边栏推荐
- Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
- 关于堆的判断 (25 分) 两种插入方式
- Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
- Study notes of deep learning (8)
- Wechat: get the owner of a single tag
- Notes d'apprentissage oneflow: de functor à opexprinterpreter
- Judgment on heap (25 points) two insertion methods
- L2-023 graph coloring problem (25 points) (graph traversal)
- 还原二叉树 (25 分)
- Error: cannot find or load main class
猜你喜欢

L2-3 浪漫侧影 (25 分)

Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing

ESP32程序下载失败,提示超时

Multi view depth estimation by fusing single view depth probability with multi view geometry

OneFlow学习笔记:从Functor到OpExprInterpreter

共享办公室,提升入驻体验

Consensus Token:web3.0生态流量的超级入口

企业微信应用授权/静默登录

洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別

MySQL查询两张表属性值非重复的数据
随机推荐
JVM工具之Arthas使用
Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
MySQL查询两张表属性值非重复的数据
Go语言自学系列 | golang嵌套结构体
How much inventory recording does the intelligent system of external call system of okcc call center need?
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
Restore binary tree (25 points)
Type anonyme (Principes fondamentaux du Guide c)
Go语言自学系列 | golang方法
The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
Use of Arthas in JVM tools
企业微信应用授权/静默登录
valgrind和kcachegrind使用運行分析
Introduction to matlab
Single chip microcomputer nixie tube stopwatch
使用flask和h5搭建网站/应用的简要步骤
Valgrind and kcache grind use run analysis
bashdb下载安装
[original] use system Text. JSON formats the JSON string
Failed to prepare device for development