当前位置:网站首页>go 指针
go 指针
2022-07-05 13:00:00 【UPythonFish】
go 指针
指针是一种存储变量内存地址(Memory Address)的变量。在学习指针之前,建议先记住下面3句话。
1. 类型前放 * 表示指针类型,这个类型的指针,指向这个类型的指针
2. 在变量前加 & 取地址符号,表示取该变量的地址
3. 在(指针)变量前加 * 表示反解,把地址解析成具体的值
定义指针
指针变量的类型为 *T,该指针指向一个 T 类型的变量。
func main() {
var a int = 10 // 定义一个int类型的变量
var p *int // 定义一个整数类型的指针
p = &a //把a的地址给p
fmt.Println("a的地址为:",p) // a的地址为: 0xc00000a098
}
指针的零值与解引用
指针是引用类型,在go中,所有引用类型的零值都为 nil
指针的解引用可以获取指针所指向的变量的值。将 a 解引用的语法是 *a。
func main() {
var a int = 10 // 定义一个int类型的变量
var p *int // 定义一个整数类型的指针
if p == nil {
fmt.Println("指针的零值为nil") // 指针的零值为nil
}
p = &a //把a的地址给p
fmt.Println("p的解引用值为:", *p) // p的解引用值为: 10
}
指针也是一个变量,因此也存在对应的内存地址,所以就会存在 指针的指针
func main() {
var a int = 10 // 定义一个int类型的变量
var p *int // 定义一个整数类型的指针
var m **int
p = &a //把a的地址给p
m = &p //把p的地址给m
fmt.Println("p的解引用值为:", *p) // p的解引用值为: 10
fmt.Println("m的解引用值为:", *m) // m的解引用值为: 0xc0000aa058 -> p的值
fmt.Println("a的值为:", **m) // a的值为: 10
}
并且能够一直嵌套下去,也能够反解出值
函数传递指针参数
可以向函数传递指针类型参数,并且可以通过指针修改值,影响外部变量的值
func main() {
a := 10
p := &a
test(p)
fmt.Println(a) // 99
}
func test(p *int) {
fmt.Println(p) // 0xc00000a098
fmt.Println(*p) // 10
*p = 99 // 修改值,不是改p,改p指向的值也就是a的值
}
需要注意的是,不要向函数传递数组的指针,而应该使用切片,使用切片的好处是,如果数组长度变了,函数不需要重写,而如果使用数组的指针,则需要重写函数。
func main() {
var a = [3]int{
7,8,9}
var p *[3]int = &a
test2(p)
test3(a[:])
}
func test3(a []int) {
fmt.Println(a) // [7 8 9]
}
func test2(a *[3]int) {
fmt.Println(*a) // [7 8 9]
}
如果数组长度变成4,代码如下
func main() {
var a = [4]int{
7,8,9}
var p *[4]int = &a
test2(p)
test3(a[:])
}
func test3(a []int) {
// 去切片不需要修改
fmt.Println(a)
}
func test2(a *[4]int) {
// 函数必须修改,才能使用
fmt.Println(*a)
}
如果是数组的指针,不需要解引用,直接使用即可,按索引取值即可
func main() {
var a = [4]int{
7,8,9}
test2(p)
}
func test2(a *[4]int) {
fmt.Println(a[0]) // 7
fmt.Println(a[1]) // 8
}
指针数组与数组指针
指针数组指数组里面放了一堆指针,数组指针指指向数组的指针
func main() {
var a = [4]int{
7,8,9}
// 数组指针
p := &a
}
// 指针数组
func test4(p *[4]int) {
fmt.Println((*p)[1]) // 先对a解引用--->数组--->再取值-->8
// (*p)[0]=10 先解引用,再修改 两种写法都可以
p[0]=10 // 把数组第0个位置的值改成了 10
fmt.Println(*p)
}
边栏推荐
- 程序员成长第八篇:做好测试工作
- 前缀、中缀、后缀表达式「建议收藏」
- My colleague didn't understand selenium for half a month, so I figured it out for him in half an hour! Easily showed a wave of operations of climbing Taobao [easy to understand]
- Difference between avc1 and H264
- Why is your next computer a computer? Explore different remote operations
- Put functions in modules
- mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET
- Hiengine: comparable to the local cloud native memory database engine
- How do e-commerce sellers refund in batches?
- LB10S-ASEMI整流桥LB10S
猜你喜欢

Navigation property and entityset usage in SAP segw transaction code

Get to know linkerd project for the first time

Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications

Introduction to sap ui5 flexiblecolumnlayout control

ASEMI整流桥HD06参数,HD06图片,HD06应用

Word document injection (tracking word documents) incomplete

Concurrent performance test of SAP Spartacus with JMeter

From the perspective of technology and risk control, it is analyzed that wechat Alipay restricts the remote collection of personal collection code

无密码身份验证如何保障用户隐私安全?

How to protect user privacy without password authentication?
随机推荐
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
Halcon template matching actual code (I)
《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
Laravel document reading notes -mews/captcha use (verification code function)
How to realize batch sending when fishing
Navigation property and entityset usage in SAP segw transaction code
简单上手的页面请求和解析案例
#yyds干货盘点# 解决名企真题:搬圆桌
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
OpenHarmony应用开发之Navigation组件详解
Didi open source Delta: AI developers can easily train natural language models
APICloud Studio3 WiFi真机同步和WiFi真机预览使用说明
From the perspective of technology and risk control, it is analyzed that wechat Alipay restricts the remote collection of personal collection code
无密码身份验证如何保障用户隐私安全?
uni-app开发语音识别app,讲究的就是简单快速。
Install rhel8.2 virtual machine
MySQL 巨坑:update 更新慎用影响行数做判断!!!
Small case of function transfer parameters
RHCSA5