当前位置:网站首页>Go language pointer, value reference and pointer reference
Go language pointer, value reference and pointer reference
2022-06-24 23:29:00 【fenglllle】
Preface
Recent projects , Development go Of sdk, Which involves interface programming , similar typescript, The pointer , Pointer types . It's like C Same pronunciation , Pointers involve pointer references .
stay go In language
* type Indicates that the current type is a pointer type on the type , Represents the value of a pointer on a variable
& Variable Indicates a pointer to the current variable
demo
Use the structure to simulate :
package main
import "fmt"
type Man struct {
name string
age int
}
func sayHello(m Man) string {
m.name = "not tom"
return "hello," + m.name
}
func sayHi(m *Man) string {
m.name = "jim"
return "hi, " + m.name
}
func main() {
man := Man{
name: "tom",
age: 26,
}
fmt.Println(sayHello(man))
fmt.Println(man.name)
fmt.Println("---------------------")
fmt.Println(sayHi(&man))
fmt.Println(man.name)
}simulation demo, One uses structural parameters , A parameter that uses a pointer , give the result as follows
You can see that the structure is used to pass , Values modified in the function are not passed outside the function , The structure passed through the pointer type can modify the value of the structure . It can be explained that the parameters of the function are actually copies of the structure of the stack reference , Only modify the pointer , That is, the value of the data structure stored in the memory address can really modify the data .
If the basic data type is used , Results the same , In reflection, the pointer is used to set the value , To modify it successfully , It's the same thing .

contrast Java voice
The author is in 2018 In writing the Java Whether the modification of the parameter value by the voice method affects the external value , It's actually the same , All roads lead to Rome , however Java The language cannot directly manipulate the pointer of the basic data type , Only pointers to referenced objects can be manipulated , To some extent , A reference object is a pointer , actually Java That's how it's done , Method execution , The stack variable points to the memory address of the heap object .
go The language can directly manipulate pointers of basic data types, and it is also very flexible , This is Java Do not have the ,Java You need to encapsulate the object , It may also be determined by object-oriented design ,go Is interface oriented , The duck type . That is, the same behavior is considered a kind of .
summary
In fact, many phonetics can be used for reference , Sometimes designs come to the same end by different paths , A typical example is the algorithm , Any language , The result of the algorithm is the same .& Address fetch ,* Represents the value of the pointer

边栏推荐
- Installation and deployment of ganglia
- 中学校园IP网络广播系统解决方案-校园数字IP广播系统方案设计指南
- 斐波那契
- R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses exp function, confint function and coef function to obtain the confidence
- #22Map介绍与API
- Bubble sort
- 257. 关押罪犯
- MySQL 表的增删查改
- 376. machine tasks
- 7-8 循环日程安排问题
猜你喜欢

伪原创智能改写api百度-收录良好

【UVM入门 ===> Episode_8 】~ Sequence 和 Sequencer、Sequence 层次化

22map introduction and API

【js】-【数组、栈、队列、链表基础】-笔记

7-6 铺设油井管道

Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine

Latest development of jetpack compose
![[JS] - [string - application] - learning notes](/img/dc/f35979b094f04c0ee13b3354c7741d.png)
[JS] - [string - application] - learning notes

Installing IBM CPLEX academic edition | CONDA installing CPLEX

Blogs personal blog project details (servlet implementation)
随机推荐
Gocolly manual
golang map clear
冒泡排序
第六章 网络学习相关技巧5(超参数验证)
企业数据防泄露解决方案分享
Spark's wide dependence and narrow dependence yyds dry goods inventory
Laravel pagoda security configuration
【js】-【數組、棧、隊列、鏈錶基礎】-筆記
File contains vulnerability issues
監聽 Markdown 文件並熱更新 Next.js 頁面
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数、confint函数、coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比的置信区间
Dig deep into MySQL - resolve the clustered index / secondary index / federated index of InnoDB storage engine
MySQL semi sync replication
Installing IBM CPLEX academic edition | CONDA installing CPLEX
Latest development of jetpack compose
sql -CONVERT函数
Financial management [5]
Ningde times will increase RMB 45billion: Hillhouse subscribes RMB 3billion and Zeng Yuqun still controls 23% of the equity
基本数据类型
376. 机器任务