当前位置:网站首页>[golang learning notes] is parameter transfer in go language value transfer or reference transfer
[golang learning notes] is parameter transfer in go language value transfer or reference transfer
2022-07-23 22:05:00 【Vivien_ oO0】
List of articles
Preface
Go All parameters in are passed Value passed , It is a copy of data . Specifically, it is divided into reference type and non reference type
Non reference type ( Value type ):int,string,float,bool, Array and struct;
characteristic : After value type variable declaration , What is stored directly is the corresponding data .
Reference type : The pointer ,slice,map,channel, Interface , Functions, etc .
characteristic : The variable stores a memory address value , The space pointed to by this address value is the final value . Memory is usually allocated in the heap , When no task variable references this address , The data space corresponding to the address becomes garbage , adopt GC Recycling .
Here I'm talking about value passing , It means that the reference type is passed , In fact, it is the actual address of the value , That is, a copy of the address stored in the current variable is transferred .
Value passed
Code :
package main
import "fmt"
func main() {
var value = 3
// because int Value type So you need to get the address character
fmt.Printf("modify before addr: %p value: %d\n", &value, value)
modify(value)
// Print again
fmt.Printf("modify after main addr: %p value: %d\n", &value, value)
}
func modify(value int) {
value = 0
fmt.Printf("modify addr: %p value: %d\n", &value, value)
}
Output :
modify before addr: 0xc000018080 value: 3
modify addr: 0xc000018088 value: 0
modify after main addr: 0xc000018080 value: 3
It can be found that the two addresses are not the same address , And the arguments will not be modified
reference
At this time, we use slice Give an example
Code :
package main
import "fmt"
func main() {
value := []int{
1, 2, 3}
fmt.Printf("modify begin addr: %p value: %d\n", value, value)
fmt.Printf("modify begin addr: %p value: %d\n", &value, value)
modify1(value)
fmt.Printf("modify after addr: %p value: %d\n", value, value)
fmt.Printf("modify after addr: %p value: %d\n", &value, value)
}
func modify1(value []int) {
value[0] = 0
fmt.Printf("modify addr: %p value: %d\n", value, value)
fmt.Printf("modify addr: %p value: %d\n", &value, value)
}
result :
modify begin addr: 0xc0000b4000 value: [1 2 3]
modify begin addr: 0xc0000a4018 value: [1 2 3]
modify addr: 0xc0000b4000 value: [0 2 3]
modify addr: 0xc0000a4078 value: [0 2 3]
modify after addr: 0xc0000b4000 value: [0 2 3]
modify after addr: 0xc0000a4018 value: [0 2 3]
You can find 
This address is the same whether it is an argument or an internal address of a function , The other address is different .
This is because slice is a reference type , Then the address of the actual data is stored in this variable , When passing parameters , What we send is the saved address , Instead of saving this address . That is to say, we passed this address value , Then a memory is opened inside the function to store this address , Then we can see whether it is outside or inside , The address value passed is unchanged . The address where this address is saved is changed . That's why golang Is the reason for value transmission .

So modify the data in the function , In fact, it is to modify the data in the saved address , So it will affect the outside of the function .
边栏推荐
- Golang invalid argument to intn报错的解决
- Several methods of obtaining longitude and latitude by cesium
- 欧氏聚类(API)及其单木分割
- Complete set of official openlayers instances
- Still have 1 requests outstanding when connection from slaveX/X.X.X.X:33202 is closed
- Jmeter性能综合实战——签到及批量签到
- Comment forcer complètement le meurtre de processus indépendants de l'arrière - plan?
- Comparison of open source distributed link tracking
- [acwing] weekly competition
- Lambda learning (the use of comparator after sort and collectors.groupingby after collection)
猜你喜欢

Introduction to I2C Principle & Application of esp32

ESP32 的 I2C 原理 & 应用入门

uniapp使用canvas写环形进度条

JS——事件代理和应用场景

Uniapp uses canvas to write a circular progress bar

STM32单片机使用ADC功能驱动手指检测心跳模块

【数学建模暑期培训】配送中心选址问题

性能测试知识应用于实战

I, AI doctoral student, online crowdfunding research topic

Altium designer—Arduino UNO原理图&PCB图(自制Arduino板)
随机推荐
[create birthday card application]
王学岗视频编码————MediaCodec编解码
Interval DP chain stone merging
How to implement desktop lyrics in pyqt
lambda學習(sort後面的Comparator的使用,collection後使用Collectors.groupingBy分組)
-2021 sorting and sharing of the latest required papers related to comparative learning
10道面试基础笔试题,你能对几题?
Yolo7 mask recognition practice
Several methods of obtaining longitude and latitude by cesium
Storage structure and management disk. It's a bit like installing Win98. You need to partition and format the hard disk first
lambda学习(sort后面的Comparator的使用,collection后使用Collectors.groupingBy分组)
Quick review of interview (III): probability theory and mathematical statistics
Pulsar open source message queue_ Understand pulsar --- pulsar work notes 001
Complete set of official openlayers instances
Neo4j应用
Use code to set activity to transparent
Leaderboard design in game server
Yuanqi Digitalization: existing mode or open source innovation Lixia action
Cesium keyboard and mouse control camera roaming (source code + principle explanation)
DBSCAN点云聚类