当前位置:网站首页>Go language self-study series | golang structure as function parameter
Go language self-study series | golang structure as function parameter
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
go Structures can be like ordinary variables , As an argument to a function , Pass to function , There are two situations :
- Direct transfer structure , This is a copy ( Copy ), The contents of the outer structure will not be changed inside the function
- Pass structure pointer , Inside the function , Able to change the content of external structure
Direct transfer structure
example
package main
import "fmt"
type Person struct {
id int
name string
}
func showPerson(person Person) {
person.id = 1
person.name = "kite"
fmt.Printf("person: %v\n", person)
}
func main() {
person := Person{1, "tom"}
fmt.Printf("person: %v\n", person)
fmt.Println("-------------------")
showPerson(person)
fmt.Println("-------------------")
fmt.Printf("person: %v\n", person)
}
Running results
[Running] go run "/Users/guoliang/SynologyDrive/ software development /go/golang Introduction to project practice /goproject/360duote.com/pro01/tempCodeRunnerFile.go"
person: {1 tom}
-------------------
person: {1 kite}
-------------------
person: {1 tom}
As you can see from the results , The contents of the structure are changed inside the function , The outside of the function has not been changed
Pass structure pointer
example
package main
import "fmt"
type Person struct {
id int
name string
}
func showPerson(person *Person) {
person.id = 1
person.name = "kite"
fmt.Printf("person: %v\n", person)
}
func main() {
person := Person{1, "tom"}
fmt.Printf("person: %v\n", person)
fmt.Println("-------------------")
showPerson(&person)
fmt.Println("-------------------")
fmt.Printf("person: %v\n", person)
}
Running results
[Running] go run "/Users/guoliang/SynologyDrive/ software development /go/golang Introduction to project practice /goproject/360duote.com/pro01/test.go"
person: {1 tom}
-------------------
person: &{1 kite}
-------------------
person: {1 kite}
版权声明
本文为[COCOgsta]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230822135817.html
边栏推荐
猜你喜欢
随机推荐
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
Introduction to GUI programming swing
洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
2021李宏毅机器学习之Adaptive Learning Rate
Solidity 问题汇总
bashdb下载安装
PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
Type anonyme (Principes fondamentaux du Guide c)
关于cin,scanf和getline,getchar,cin.getline的混合使用
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
Harbor企业级镜像管理系统实战
怎样读取Excel表格到数据库
Use of Arthas in JVM tools
Correct method of calculating inference time of neural network
Summary of solid problems
政务中台研究目的建设目标,建设意义,技术创新点,技术效果
Wechat: get the owner of a single tag
BK3633 规格书
关于堆的判断 (25 分) 两种插入方式









