当前位置:网站首页>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
边栏推荐
- 耳穴减肥自身感受细节描述0422
- CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association
- Shell script advanced
- input元素添加监听事件
- QFileDialog select multiple files or folders
- Reference passing 1
- 匿名類型(C# 指南 基礎知識)
- Detailed description of self feeling of auricular point weight loss 0422
- Anonymous type (c Guide Basics)
- LINQ学习系列-----1.4 匿名对象
猜你喜欢
![[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel](/img/7c/0adc0940b6d5c8a61d34bfa5f66ee7.png)
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel

项目上传部分

分布式消息中间件框架选型-数字化架构设计(7)

MATLAB 画五星红旗

Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting

Transformer XL: attention language modelsbbeyond a fixed length context paper summary

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

洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解

396. Rotate Function

【深度好文】Flink SQL流批⼀体化技术详解(一)
随机推荐
Copy array in JS
程序,进程,线程;内存结构图;线程的创建和启动;Thread的常用方法
Stm32f103zet6 [development of standard library functions] - Introduction to library functions
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
记录:js删除数组中某一项或几项的几种方法
rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果
线程的调度(优先级)
Overview of bus structure
STM32使用HAL库,整体结构和函数原理介绍
PgSQL wants to implement all kinds of column sub query operations of MySQL
DOM 学习之—添加+-按钮
Asan minimalism
关于cin,scanf和getline,getchar,cin.getline的混合使用
二维01背包
'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。
Data deletion and modification (MySQL)
四张图弄懂matplotlib的一些基本用法
RPC procedure
Redis master-slave server problem