当前位置:网站首页>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
边栏推荐
- Idempotency practice operation, explaining idempotency based on business
- Punch in: 4.23 C language chapter - (1) first knowledge of C language - (12) structure
- Supersocket is Use in net5 - startup
- Generate QR code through zxing
- 超好用的【通用Excel导入功能】
- JS takes out the same elements in two arrays
- “如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
- Basic use of Charles
- 2021-08-11
- Supersocket is Use in net5 - concept
猜你喜欢
Explanation keyword of MySQL
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
Eight elder brothers chronicle [4]
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
JS inheritance
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
Visual programming - Experiment 1
Visual programming - Experiment 2
MySQL之explain关键字详解
. net 5 Web custom middleware implementation returns the default picture
随机推荐
General testing technology [1] classification of testing
场景题:A系统如何使用B系统的页面
Course design of Database Principle -- material distribution management system
JS takes out the same elements in two arrays
Visual programming -- how to customize the mouse cursor
JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
Generate QR code through zxing
MySQL keyword group_ Concat, combined connection query
Log4net is in Net core usage
Fiddler use
IDEA查看历史记录【文件历史和项目历史】
Basic use of Charles
Unity games and related interview questions
JSON related
Iotos IOT middle platform is connected to the access control system of isecure center
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
C set
poi根据数据创建导出excel
批量下载文件----压缩后再下载
Punch in: 4.22 C language chapter - (1) first knowledge of C language - (11) pointer