当前位置:网站首页>Go语言自学系列 | golang结构体作为函数参数
Go语言自学系列 | golang结构体作为函数参数
2022-04-23 08:22:00 【COCOgsta】
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:Go语言自学系列 | 汇总_COCOgsta的博客-CSDN博客
go结构体可以像普通变量一样,作为函数的参数,传递给函数,这里分为两种情况:
- 直接传递结构体,这是一个副本(拷贝),在函数内部不会改变外面结构体内容
- 传递结构体指针,这时在函数内部,能够改变外部结构体内容
直接传递结构体
实例
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] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/tempCodeRunnerFile.go"
person: {1 tom}
-------------------
person: {1 kite}
-------------------
person: {1 tom}
从运行结果可以看出,函数内部改变了结构体内容,函数外面并没有被改变
传递结构体指针
实例
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] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
person: {1 tom}
-------------------
person: &{1 kite}
-------------------
person: {1 kite}
版权声明
本文为[COCOgsta]所创,转载请带上原文链接,感谢
https://blog.csdn.net/guolianggsta/article/details/124355591
边栏推荐
- Get the absolute path of the class according to the bytecode
- SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
- Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
- 匿名类型(C# 指南 基础知识)
- 如何保护开源项目免遭供应链攻击-安全设计(1)
- MySQL数据库中delete、truncate、drop原理详解
- LeetCode-199-二叉树的右视图
- Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
- The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
- QT compilation qtxlsx Library
猜你喜欢
随机推荐
form表单 post提交 数据量大的问题
00后最关注的职业:公务员排第二,第一是?
耳穴减肥自身感受细节描述0422
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
RPC procedure
Generate and parse tokens using JWT
正点原子携手OneOS直播 OneOS系统教程全面上线
pgsql想实现mysql一样样的列子查询操作
5.6 comprehensive case - RTU-
Word plus watermark
Notes on English class (4)
请问中衍期货安全靠谱吗?
【深度好文】Flink SQL流批⼀体化技术详解(一)
Ear acupoint diagnosis and treatment essay 0421
匿名類型(C# 指南 基礎知識)
Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
How to generate assembly file
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
数论求a^b(a,b为1e12级别)的因子之和
增强现实技术是什么?能用在哪些地方?