How to pass a pointer by value

polaris · · 504 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, I want to do this</p> <pre><code>func AddTask(task *Task){ //This must be a pointer StartTask(task) //... } func StartTask(task Task){ //... mySlice = append(mySlice, task) //... } </code></pre> <p>This is way simplified but Ï need a pointer in AddTask because it&#39;s a function exposed via net/RPC and I cannot have a pointer in StartTask because I use it in the builtin function append.</p> <p>Basicly I need a copy by value of a pointer.</p> <hr/>**评论:**<br/><br/>Manbeardo: <pre><p>Sounds like you&#39;re trying to dereference the pointer? You can do that with the <code>*</code> operator:</p> <pre><code>var value int var ptr *int ptr = &amp;value // get the address of value value = *ptr // dereference ptr </code></pre></pre>rjw57: <pre><p><a href="https://www.golang-book.com/books/intro/8#section1" rel="nofollow">https://www.golang-book.com/books/intro/8#section1</a></p> <blockquote> <p><code>*</code> is also used to “dereference” pointer variables. Dereferencing a pointer gives us access to the value the pointer points to. </p> </blockquote></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

504 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传