Go 自学:pointer指针

以下代码中,我们建立指针ptr指向myNumber的地址。

*ptr代表指针指向的值。

我们改变指针*ptr,可以改变被指向的变量的值。

go 复制代码
package main

import "fmt"

func main() {
	myNumber := 23

	var ptr = &myNumber

	fmt.Println("Value of the address is ", ptr)
	fmt.Println("Value of actual pointer is ", *ptr)

	*ptr = *ptr + 2
	fmt.Println("New value is: ", myNumber)
}

输出为:

Value of the address is 0xc00000a0a8

Value of actual pointer is 23

New value is: 25

相关推荐
李妍.4 小时前
02Numpy基础(上)
开发语言·python
TheBestRucy4 小时前
Python 九阳神功之贰:面向对象(下)
开发语言·python
AI_小站5 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
笃行3505 小时前
向量数据库单独建一套,这笔账划不划算
后端
felixking5 小时前
C++20 协程
开发语言·c++·协程
weixin_431600446 小时前
Agent Workflow 学习向:Code 节点,比模板更强的变量加工
后端·python·学习·ai·
Rain的Java大神之路6 小时前
短信接口被狂刷怎么处理
java·运维·后端·web安全·面试·架构·xss
Zane1994127 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言
Lost of 程序猿7 小时前
AOP 实战:面向切面编程从理论到落地
后端·asp.net·aop·面向切片变成
Lost of 程序猿7 小时前
ASP.NET Core 认证授权实战:从 JWT 到 Policy,设计一套企业级 RBAC 权限系统
后端·asp.net