当前位置:网站首页>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
边栏推荐
猜你喜欢

洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置

汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
![Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]](/img/67/1f9df4236b0aac3480836d45ab8561.png)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

Valgrind and kcache grind use run analysis

L2-024 部落 (25 分)(并查集)

flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】

STM32 uses Hal library. The overall structure and function principle are introduced

Use of Arthas in JVM tools

论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》

OneFlow学习笔记:从Functor到OpExprInterpreter
随机推荐
Enterprise wechat application authorization / silent login
微信:获取单个标签所有人
L2-024 部落 (25 分)(并查集)
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
Swagger document export custom V2 / API docs interception
Whether the same binary search tree (25 points)
共享办公室,提升入驻体验
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
Use include in databinding
数字政府建设中政务中台中的技术创新点
Share the office and improve the settled experience
根据后序和中序遍历输出先序遍历 (25 分)
swagger文档导出自定义v2/api-docs拦截
Learn SQL injection in sqli liabs (Level 11 ~ level 20)
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
Play with binary tree (25 points)
Research purpose, construction goal, construction significance, technological innovation, technological effect
GUI编程简介 swing
爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
引用传递1