当前位置:网站首页>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)
}
边栏推荐
- Realize the addition of all numbers between 1 and number
- mysql拆分字符串做条件查询
- 解决uni-app配置页面、tabBar无效问题
- RHCSA2
- Write macro with word
- LB10S-ASEMI整流桥LB10S
- Apicloud studio3 API management and debugging tutorial
- uni-app开发语音识别app,讲究的就是简单快速。
- Le rapport de recherche sur l'analyse matricielle de la Force des fournisseurs de RPA dans le secteur bancaire chinois en 2022 a été officiellement lancé.
- 跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
猜你喜欢

Flutter 绘制波浪移动动画效果,曲线和折线图

Reverse Polish notation

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

RHCSA9

OpenHarmony应用开发之Navigation组件详解

MySQL 巨坑:update 更新慎用影响行数做判断!!!
![leetcode:221. Maximum square [essence of DP state transition]](/img/ea/158e8659657984794c52a0449e0ee5.png)
leetcode:221. Maximum square [essence of DP state transition]

量价虽降,商业银行结构性存款为何受上市公司所偏爱?

Alibaba cloud SLB load balancing product basic concept and purchase process

Developers, is cloud native database the future?
随机推荐
Le rapport de recherche sur l'analyse matricielle de la Force des fournisseurs de RPA dans le secteur bancaire chinois en 2022 a été officiellement lancé.
CloudCompare——点云切片
Didi open source Delta: AI developers can easily train natural language models
Laravel document reading notes -mews/captcha use (verification code function)
Talk about my drawing skills in my writing career
Concurrent performance test of SAP Spartacus with JMeter
SAP UI5 ObjectPageLayout 控件使用方法分享
Default parameters of function & multiple methods of function parameters
Small case of function transfer parameters
百日完成国产数据库opengausss的开源任务--openGuass极简版3.0.0安装教程
Apicloud studio3 API management and debugging tutorial
将函数放在模块中
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
JXL notes
Compile kernel modules separately
RHCSA4
APICloud Studio3 API管理与调试使用教程
Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
Why is your next computer a computer? Explore different remote operations
Reflection and imagination on the notation like tool