当前位置:网站首页>Chapter VI, Section III pointer
Chapter VI, Section III pointer
2022-04-23 03:27:00 【Python code doctor】
A pointer is a value that represents a memory address . This memory address is often the starting location of the value of another variable stored in memory .Go Language support for pointers is between Java Language and C/C++ Between languages , It didn't want to Java Language like that cancels the ability of the code to directly operate the pointer , Also avoided C/C++ The security and reliability problems caused by the abuse of pointer in language .
Basic operation
Go Although the language retains the pointer , But unlike other programming languages :
- The default value is nil, No, NULL Constant
- The operator “&” Take the variable address , “*” Access the target object through the pointer
- Pointer operation not supported , I won't support it “->” Operator , direct ⽤ “.” Visit the target members
new function
expression new(T) Will create a T Anonymous variable of type , What has been done is to T New value of type allocates and clears a block of memory space , Then return the address of this memory space as the result , And the result is pointing to this new T Pointer value of type value , The returned pointer type is *T.
func main() {
var p1 *int
p1 = new(int) //p1 by *int type , Point to anonymous int Variable
fmt.Println("*p1 = ", *p1) //*p1 = 0
p2 := new(int) //p2 by *int type , Point to anonymous int Variable
*p2 = 111
fmt.Println("*p2 = ", *p2) //*p1 = 111
}
We just need to use new() function , Don't worry about the life cycle of its memory or how to delete it , because Go Language memory management system will help us to take care of everything .
Pointer as function parameter
func swap01(a, b int) {
a, b = b, a
fmt.Printf("swap01 a = %d, b = %d\n", a, b)
}
func swap02(x, y *int) {
*x, *y = *y, *x
}
func main() {
a := 10
b := 20
//swap01(a, b) // Value passed
swap02(&a, &b) // Variable address passing
fmt.Printf("a = %d, b = %d\n", a, b)
}
版权声明
本文为[Python code doctor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220619244227.html
边栏推荐
- js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
- Iotos IOT middle platform is connected to the access control system of isecure center
- Flink实时数仓项目—DWS层设计与实现
- Super easy to use [general excel import function]
- Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
- 12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
- Supersocket is Use in net5 - concept
- 2021-08-31
- When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
- 2022 group programming ladder simulation match 1-8 are prime numbers (20 points)
猜你喜欢

Punch in: 4.22 C language chapter - (1) first knowledge of C language - (11) pointer

可以接收多種數據類型參數——可變參數

Charles uses three ways to modify requests and responses

Seminar playback video: how to improve Jenkins' ability to become a real Devops platform

"Visual programming" test paper

Un aperçu des flux d'E / s et des opérations de fichiers de classe de fichiers

Code forces round # 784 (DIV. 4) solution (First AK CF (XD)

2022 团体程序设计天梯赛 模拟赛 L1-7 矩阵列平移 (20 分)

Comprehensive calculation of employee information

Using swagger in. Net5
随机推荐
socket编程 send()与 recv()函数详解
关于idea调试模式下启动特别慢的优化
Téléchargement en vrac de fichiers - téléchargement après compression
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
Generate QR code through zxing
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
Code forces round # 784 (DIV. 4) solution (First AK CF (XD)
批量下載文件----壓縮後再下載
Unity knowledge points (ugui)
Unity basics 2
L3-011 直捣黄龙 (30 分)
C set
场景题:A系统如何使用B系统的页面
Unity knowledge points (ugui 2)
Supersocket is Used in net5 - command
C-11 problem I: find balloon
JS calculates the display date according to the time
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
月薪10k-20k都无法回答的事务问题,你会吗?