当前位置:网站首页>[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 .
边栏推荐
- 欧氏聚类(API)及其单木分割
- 王学岗视频编码————MediaCodec编解码
- U++ events
- A stack of digital robots were selected in Gartner's China AI market guide
- 程序员成长第二十六篇:如何开好每日晨会?
- Use of cjson Library
- Introduction to I2C Principle & Application of esp32
- Programmer growth Article 26: how to hold a good daily morning meeting?
- How does MySQL prepare SQL (solve the problem that in query SQL preprocessing can only query one record)
- 除了钱,创业者还需要什么?专访明月湖创赛创投机构
猜你喜欢

Preliminary discussion on POC compilation

Cookies and sessions

Record the process of the first excavation and intersection

Storage structure and management disk. It's a bit like installing Win98. You need to partition and format the hard disk first

DBSCAN point cloud clustering

zk 是如何解决脑裂问题的

How does MySQL prepare SQL (solve the problem that in query SQL preprocessing can only query one record)

VLAN comprehensive experiment

Explain NAT technology in detail

prime_ series_ level-1
随机推荐
Construction and application progress of ten billion level knowledge map of meituan brain
Mqtt connection, subscription and publishing can be realized without mqtt C library
Lambda learning (the use of comparator after sort and collectors.groupingby after collection)
YOLO7 口罩识别实战
Quick review of interview (III): probability theory and mathematical statistics
还在为XShell破解烦恼,试试tabby
[hiflow] Tencent cloud's new generation of automation assistant, which I used to complete the enterprise epidemic prompt (no code)
Ali onedate's layered thought
Cesium keyboard and mouse control camera roaming (source code + principle explanation)
实验设计
U++学习笔记 基础人物轴绑定及映射绑定
MySQL的JDBC编程
除了钱,创业者还需要什么?专访明月湖创赛创投机构
Leaderboard design in game server
[complex overloaded operator]
MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)
Altium Designer - schematic diagram of Arduino uno & PCB diagram (self-made Arduino board)
Detailed explanation of cesium events (mouse events, camera events, keyboard events, scene trigger events)
存储结构和管理盘。有点像装win98要先分区格式化硬盘
程序员成长第二十六篇:如何开好每日晨会?