Can someone explain the difference between & and * in GO lang .. and provide examples of when & and * would be used to illustrate the difference? From what I have read, they both relate to accessing a variables memory location however i'm not sure when to use & or *.
6条回答 默认 最新
- 关注
码龄 粉丝数 原力等级 --
- 被采纳
- 被点赞
- 采纳率
duanping6698 2015-10-21 02:18最佳回答 专家已采纳Here is a very simple example, that illustrates how
&
and*
are used. Note that*
can be used for two different things 1) to declare a variable to be a pointer 2) to dereference a pointer.package main import "fmt" func main() { b := 6 var b_ptr *int // *int is used delcare variable // b_ptr to be a pointer to an int b_ptr = &b // b_ptr is assigned value that is the address // of where variable b is stored // Shorhand for the above two lines is: // b_ptr := &b fmt.Printf("address of b_ptr: %p ", b_ptr) // We can use *b_ptr get the value that is stored // at address b_ptr, or dereference the pointer fmt.Printf("value stored at b_ptr: %d ", *b_ptr) }
Result:
address of b_ptr: 0xc82007c1f0 value stored at b_ptr: 6
采纳该答案 已采纳该答案 专家已采纳评论解决 无用打赏举报微信扫一扫
分享评论登录 后可回复...
查看更多回答(5条)
报告相同问题?
提交
相关推荐 更多相似问题
- 2015-10-20 17:17回答 6 已采纳 Here is a very simple example, that illustrates how & and * are used. Note that * can be used for
- 2015-11-15 17:29回答 1 已采纳 A *string is a pointer to a string. If you're not familiar with pointers, let's just say that it's
- 2018-02-13 14:26回答 1 已采纳 for{} uses 100% CPU because it continuously executes the loop iteration. select{} uses nearly 0%
- 2021-04-06 15:22Aisuruki的博客 &符号的意思是对变量取地址 *符号的意思是对指针取值 例子 func main() { var a *int // 存储的是int的指针,目前为空 var b int = 4 // 存储的是int的值 a = &b // a 指向 b 的地址 ...
- 2019-04-14 20:28pengone的博客 &符号的意思是对变量取地址 *符号的意思是对指针取值
- 2020-03-28 11:25程序员阿俊的博客 *和&的区别 : & 是取地址符号 , 即取得某个变量的地址 , 如 ; &a *是指针运算符 , 可以表示一个变量是指针类型 , 也可以表示一个指针变量所指向的存储单元 , 也就是这个地址所存储的值 . 从代码中验证 ...
- 2018-11-22 09:36回答 2 已采纳 Lock(): only one go routine read/write at a time by acquiring the lock. RLock(): multiple go rout
- 2018-08-13 18:10回答 1 已采纳 Both Warn and Warning are the same. Check this https://github.com/sirupsen/logrus/blob/e4b0c6d782
- 2017-01-11 13:58回答 2 已采纳 When you open a local HTML file in your browser, then the browser will read the HTML file locally,
- 2020-09-01 15:38BeginnerXiao的博客 *和&的区别 : & 是取地址符号 , 即取得某个变量的地址 , 如 ; &a *是指针运算符 , 可以表示一个变量是指针类型 , 也可以表示一个指针变量所指向的存储单元 , 也就是这个地址所存储的值 . 代码解惑: ...
- 2019-02-21 10:27liduanwh的博客 *和聽&聽可以互相抵消,同时注意,*&可以抵消掉,但&*是不可以抵消的 a和*&a是一样的,都是a的值,值为1 (因为*&互相抵消掉了) 同理,a和*&*&...
- 2021-06-23 14:49weixin_44809746的博客 golang开发中经常用到数组。 假设我们有一个类 type Student struct { Id int64 Name string Class string Age int64 Address string ... } 开发中需要创建一个Student的数组,这时候如何选取? 直接创建 ...
- 2016-01-15 03:11回答 3 已采纳 If you are actually asking what the difference of those b's are, one is a pointer to the object pa
- 2015-10-25 01:59回答 3 已采纳 This is related to channels in Go. You are thinking it's related to assignment as in other languag
- 2016-08-19 08:09回答 1 已采纳 Well, you have said it yourself. gRPC is a framework that uses RPC to communicate. RPC is not Prot
- 2021-02-01 04:09weixin_39887961的博客 golang和python有什么区别?下面本篇文章给大家对比一下Python和Golang,介绍一下golang和python的区别。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。golang和python的区别1、范例Python是一...
- 2019-09-09 23:07江北一滴水的博客 #include<stdio.h> #include<iostream> using namespace std; int main(void) { int a=10; int *p=&a; int *&q = p; cout << "p的按地址求值"<<*&... ...
- 2021-02-27 10:07会飞的胖达喵的博客 result := gconv.Map(content) fmt.Printf("%T\n",resp) //该处&resp的一点理解,&对resp类型进行初始化(不一定都是各类型的零值),并返回指向该对象的指针 //&与new的区别,new会初始化零值,&可以预设自己的...
- 2018-11-18 11:19回答 1 已采纳 Intention of mocks and stubs in GO is the same as different programming languages: stub is repla
- 2020-09-21 05:35Go 语言中的 new 和 make 一直是新手比较容易混淆的东西,咋一看很相似。不过解释两者之间的不同也非常容易,下面这篇文章主要介绍了golang中make和new的区别,需要的朋友可以参考借鉴,下面来一起看看吧。
- 没有解决我的问题, 去提问