ELI5: Pointers/Channels

blov · · 605 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi All,</p> <p>I went through Gobyexample.com and just read through the &#34;Learn Go in Y Minutes&#34; but I&#39;m still confused by what Pointers and Channels are. I come from the NodeJS world (don&#39;t shoot me!) so still new to some of these concepts.</p> <p>Does anyone have a good ELI5/TLDR for pointers/channels?</p> <p>A simple code sample would be perfect as well!</p> <p>Thanks</p> <hr/>**评论:**<br/><br/>condanky: <pre><p>this a good tutorial for channels. <a href="http://guzalexander.com/2013/12/06/golang-channels-tutorial.html">http://guzalexander.com/2013/12/06/golang-channels-tutorial.html</a></p> <p>they allow for easy messaging between goroutines </p> <p>for pointers check out <a href="http://golangtutorials.blogspot.com/2011/06/memory-variables-in-memory-and-pointers.html">http://golangtutorials.blogspot.com/2011/06/memory-variables-in-memory-and-pointers.html</a></p> <p>there is tons of resources online. pointers are not unique to golang either, so you can reference other languages </p></pre>mc_hammerd: <pre><p>not an expert or anything but this is how i understand it</p> <p>channels are like streams (?) for: talking asynchroneously to your other threads</p> <p>when used correctly with select and range(mychan), its almost like reactive programming</p> <pre><code>strm := make(chan string) go func worker() { // new goroutine similar to a thread for { // while(1) x := readfilechecknewcontents() if x { strm &lt;- x.newdata } // write something to strm time.Sleep(1000) } }() go func reader () { select strm { // read strm case v &lt;- strm: fmt.Println(&#34;our data reader said:&#34;, v) // read value of strm // this will fire every time the other thread writes to stream. very cool case close: fmt.Println(&#34;thread closed shutting down!&#34;); break; } }() // keep doing stuff .. while those two always handle your data .. .. these two just loop forever and talk to each other (12 loc) and use like 0.5% cpu, in C this was a PITA.. </code></pre> <p>the benefit of this is the reading data and processing data are done asynchronously.</p> <hr/> <p>pointers</p> <p>pointer is a way to pass a address of a value ... so if you change the value the new value is available to the old code</p> <p>also a shorter way of writing code:</p> <pre><code> func foo(obj bar){ obj.newvalue = 4; return obj } obj = foo(obj) </code></pre> <p>shorter with pointers:</p> <pre><code> func foo(obj *bar){ obj.newvalue = 4 } foo(&amp;obj) // pass the address of obj </code></pre> <p>ex:</p> <p>no pointer:</p> <pre><code> func foo(obj bar){ obj.newvalue = 4 } obj = bar{ newvalue: 0 } foo(obj) fmt.Println(obj.newvalue) // 0 ... this didnt update </code></pre> <p>pointer:</p> <pre><code> func foo(obj *bar){ obj.newvalue = 4 } obj = bar{ newvalue: 0 } foo(&amp;obj) fmt.Println(obj.newvalue) // 4 </code></pre> <p>its also faster for big data to pass the pointer, not copying the value to the function!</p></pre>joeydj: <pre><p>If you create a new instance of your object, you will get a pointer back. That pointer represents the location of that object in your memory.</p> <pre><code>type Apple struct{} myApple := &amp;Apple{} // myApple is a pointer to the instance of Apple you created // the type of myApple would be *Apple. The * represents that this is a pointer to an instance. // It&#39;s not the apple itself </code></pre> <p>So, if you want to create a function that does something with the apple you just created, you use a pointer as a parameter:</p> <pre><code>func eat (apple *Apple) { // do something with the apple } </code></pre> <p>The function will only take an instance of Apple, that you created like in the first example. It makes sure that it works with an already existing Apple.</p> <p>I always explain channels to someone like an array. The <em>&lt;-</em> of the channels are like the push() and pop() methods. The only difference is that pop() will block your code until the array has at least 1 element that it can pop.</p> <p>But watch out: If you pop something from a channel in your main(), it will not compile. The pop would block your whole program forever. You have to have at least one part of the code that can independently push something to the channel to make sure there will eventually be something added to your channel.</p> <p>Hope this helps</p></pre>

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

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