【golog】我的go学与思7

8 篇文章 0 订阅
8 篇文章 0 订阅

指针

正文

package main

import "fmt"

func tmp() *int{
	v := 1
	return &v
}

func main() {
	i, j := 42, 2701

	p := &i         // point to i
	fmt.Println(*p) // read i through the pointer
	*p = 21         // set i through the pointer
	fmt.Println(i)  // see the new value of i

	p = &j         // point to j
	*p = *p / 37   // divide j through the pointer
	fmt.Println(j) // see the new value of j
	fmt.Println(tmp() == tmp(),*tmp())
}

//输出
42
21
73
false 1

这里和c++很相似,都有取址。但是p++这样的指针运算是非法的。

最让我费解的是返回临时变量的地址,每次不相同,但是都是对的。这边有点想不明白go的内存模型了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值