当前位置:网站首页>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
边栏推荐
- 场景题:A系统如何使用B系统的页面
- Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)
- 关于idea调试模式下启动特别慢的优化
- 12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
- QT learning summary
- 2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
- “如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
- Build websocket server in. Net5 webapi
- Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
- Code forces round # 784 (DIV. 4) solution (First AK CF (XD)
猜你喜欢

2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)

12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
![General testing technology [1] classification of testing](/img/f1/d80b6793b6443cbc4048d7e6319f51.png)
General testing technology [1] classification of testing

Explanation keyword of MySQL

Web Course Design - his system

Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)

Visual programming -- how to customize the mouse cursor

Unity games and related interview questions

Why is bi so important to enterprises?

Experiment 6 input / output stream
随机推荐
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
淺學一下I/O流和File類文件操作
. NETCORE sets the API post mode, which can accept parameters directly in parentheses
Fiddler use
Visual programming - drawing assignment
Why is bi so important to enterprises?
可以接收多種數據類型參數——可變參數
第四次作业
Code forces round # 784 (DIV. 4) solution (First AK CF (XD)
批量下载文件----压缩后再下载
TCP three handshakes and four waves
Preview of converting doc and PDF to SWF file
C set
Test questions (2)
Five tips for cross-border e-commerce in 2022
When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
2021-08-31
MySQL query specifies that a row is sorted to the first row
Redis(17) -- Redis缓存相关问题解决