A couple questions about this language

blov · · 511 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m coming from a background of php / node.js and I&#39;m really like the features golang has to offer, such as quickly handling concurrent tasks. I&#39;m running my code in the online IDE c9.io, and everytime I try running my code inside my main function:</p> <pre><code>var p *int i := 42 p = &amp;i fmt.Println(p) *p = 32; fmt.Println(p) </code></pre> <p>It outputs &#39;0xc82000a3b0&#39; for both Println statements, and I have no idea why. Another question I have is how can I serve html/php files through my go app on c9.io?</p> <hr/>**评论:**<br/><br/>kumiorava: <pre><p>Because p is an integer <em>pointer</em>. The hex value you see is the memory address it points to. To get the pointed value you need to dereference it.</p> <pre><code>fmt.Println(*p); </code></pre></pre>togamans: <pre><p>what happens if you print *p instead?</p></pre>togamans: <pre><p><a href="http://play.golang.org/p/-4rk8JcaMH" rel="nofollow">http://play.golang.org/p/-4rk8JcaMH</a></p></pre>aagee: <pre><p>very Socratic </p></pre>togamans: <pre><p>such method. </p></pre>arconx: <pre><p>You need to take in account that Go does not handle pointers automatically. Like a few other people pointed out, you used a *int ( what is a int pointer ). </p> <p>Go is not like PHP ( now ). PHP in the past also had you manually assign the pointers and if you did not it constantly made a copy of the variable. Somewhere in version 4 if i remember they altered the default behavior and made pointers the default. This of course had a nice performance gain and it reduced people there workload.</p> <p>It takes some time to wrap your head around pointers if your not used to them. But they have massive advantages.</p> <p>Example in old PHP:</p> <p>$x = 4; // 4 $y = $x; // 4 $z = $y + 1; // 5</p> <p>If you had a statement like that in the old PHP version, this will happen:</p> <p>$x = 4 ( lets say in memory address: x01 )... $y = copy of $x... 4 ( in memory address: x02 )... $z = copy of $y + 1 ... 5 ( in memory address: x03 )...</p> <p>With the &#34;never&#34; version of PHP this happened:</p> <p>$x = 4 ( lets say in memory address: x01 )... $y = 4 ( in memory address: x01!!! Notice how $y is actually using the exact same memory address as $x )... $z = copy of $y + 1 ( in memory address: x02 ). So why is $z a copy in its own address space: because the value changed, so PHP made a copy, modified the value and assigned it into its own memory address location.</p> <p>The newer PHP version took some of the work out of the developers there hands and they did in the background checks to see if values changed. If they did not change, variables simply shared the same memory location ( reducing memory need and increasing performance ).</p> <p>And now to confuse some more ( in the newer version of PHP ):</p> <p>$x = 4; // 4 $y = $x; // 4 $z = *$y + 1; // 5</p> <p>This translates to:</p> <p>$x = 4 ( lets say in memory address: x01 )... $y = 4 ( in memory address: x01! At this moment but this will change the moment we change something in $z )... $z = Has become 5 at this point BUT ... because we forced a pointer to *$y, it means we actually are updating $y. So both $z and $y become 5. So PHP will force $y to memory address x02 and $z shares this same address...</p> <p>The result is:</p> <p>$x is 4 ( memory address: x01 ) $y is 5 ( memory address: x02 ) $z is 5 ( memory address: x02 )</p> <p>... Pointers ... so much fun &gt;:)</p></pre>abi_hawkeye: <pre><p>You need to change your perspective when trying out a system programming language for the first time. (go? kinda..) Here in India students first start programming in C then C++ so on and so forth. So when he/she gets into say python they&#39;ll know all the nuts and bolts.</p></pre>Fizzyfloat: <pre><p>I thought PHP would be similar to it, but I guess I was wrong</p></pre>lolis5: <pre><p>You&#39;re not wrong. PHP and golang both have similarities with C/C++, but that can be said for most modern languages like java, and javascript. There are a few things that differ, but from a syntax perspective golang is pretty similar to the other languages you&#39;ve written in.</p> <p>The biggest new concept is the idea of a pointer. It can be a tough topic to wrap your head around if it&#39;s the first time you see it, but don&#39;t let it discourage you. It&#39;s the difference between passing the data itself, and the location of where the data is stored.</p> <p>To help you understand your code a bit:</p> <pre><code>var p *int // a pointer. This can hold the address of an int i := 42 // i is an int that exists somewhere in memory p = &amp;i // &amp;i specifies that you want to take the address of i // that address is then assigned to p. // p = the location of i fmt.Println(p) // print the value of p (the location of i) *p = 32; // * dereferences p, meaning you&#39;re saying that you // want to assign the value to what p points to, // not p. You are actually changing the value that is // stored in i. you could do a fmt.Println(*p) at // this point and see 32. fmt.Println(p) // print the value of p again, at this // point it is still the address of i. </code></pre> <p>I hope this helps.</p></pre>Fizzyfloat: <pre><p>Thanks, this did help a bunch. I am confused on why pointers exist however, it seems rather unnecessary. When would I ever need to use this feature? </p></pre>earthboundkid: <pre><p>Pointers are used so often that it&#39;s hard to give an answer to your question. But let my try to answer on two tracks.</p> <p>First, you can think about what pointers are used for in Go. Basically two things: telling the compiler to reuse memory instead of copying a variable, and allowing nullable types. In the first case, you can pretty substantially lower memory costs if you don&#39;t make unnecessary copies. The second is not as important, since you could always just make a struct like { value, ok } instead, but it&#39;s still convenient sometime when you don&#39;t want to return a value to return <code>nil</code> instead as a signal that the operation failed.</p> <p>The second way of looking at it is according to how languages ask you to look at programming. In C, you look at it from the perspective of the layout of memory in RAM in almost exhaustive depth (not quite because you don&#39;t deal with virtual memory issues, but almost exhaustive): is this value a global or on the stack or in the heap? How many bytes does it take up? Are the bytes aligned? How will we know when the memory can be reused?</p> <p>PHP is at the other extreme. You can use <code>&amp;</code> in PHP to tell it to reuse references sometimes, but other than that you&#39;re not really expected to think too much about what&#39;s going on in memory. Memory is garbage collected automatically, and basically every request gets a fresh environment to do things in.</p> <p>Go is sort of in between. You don&#39;t need to worry about stack vs. heap or alignment, and it has garbage collection, but other than that you&#39;re expected to think through the implications of memory decisions yourself. This makes it much faster and more efficient, but it can take more work, especially when you get started.</p></pre>earthboundkid: <pre><p>Adding one more thing: In C, you can only return one value from a function, so traditionally if you wanted to return multiple values, you&#39;d use pointers instead. Don&#39;t do this in Go! We don&#39;t need to waste time on that crap anymore. But if you wanted to do this for some reason, it <a href="http://play.golang.org/p/1iLtCbfweK" rel="nofollow">would look like this</a>.</p></pre>dilap: <pre><p>Pointers let you modify the pointed-to value. </p> <p>Example:</p> <p><a href="http://play.golang.org/p/pN5Ag_X7zg" rel="nofollow">http://play.golang.org/p/pN5Ag_X7zg</a></p> <p>Languages like Python and Java (and maybe PHP, but I&#39;m not familiar with it) just use pointers all the time under the covers; other languages like Go and C expose the distinction to the user.</p> <p>Using pointers all the time works fine, but has performance implications -- it&#39;s useful to be able to choose between pointer and non-pointer in your datastructures so that you can control the memory layout of your data.</p></pre>

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

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