gobyexample/public/pointers
2015-09-14 08:50:18 -07:00

194 lines
6.9 KiB
Plaintext

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Pointers</title>
<link rel=stylesheet href="site.css">
</head>
<script type="text/javascript">
if (window.location.host == "gobyexample.com") {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-34996217-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
}
</script>
<body>
<div class="example" id="pointers">
<h2><a href="./">Go by Example</a>: Pointers</h2>
<table>
<tr>
<td class="docs">
<p>Go supports <em><a href="http://en.wikipedia.org/wiki/Pointer_(computer_programming)">pointers</a></em>,
allowing you to pass references to values and records
within your program.</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="http://play.golang.org/p/KdE4TBbUL2"><img title="Run code" src="play.png" class="run" /></a>
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="s">&quot;fmt&quot;</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>We&rsquo;ll show how pointers work in contrast to values with
2 functions: <code>zeroval</code> and <code>zeroptr</code>. <code>zeroval</code> has an
<code>int</code> parameter, so arguments will be passed to it by
value. <code>zeroval</code> will get a copy of <code>ival</code> distinct
from the one in the calling function.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">zeroval</span><span class="p">(</span><span class="nx">ival</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">ival</span> <span class="p">=</span> <span class="mi">0</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p><code>zeroptr</code> in contrast has an <code>*int</code> parameter, meaning
that it takes an <code>int</code> pointer. The <code>*iptr</code> code in the
function body then <em>dereferences</em> the pointer from its
memory address to the current value at that address.
Assigning a value to a dereferenced pointer changes the
value at the referenced address.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">zeroptr</span><span class="p">(</span><span class="nx">iptr</span> <span class="o">*</span><span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="o">*</span><span class="nx">iptr</span> <span class="p">=</span> <span class="mi">0</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">i</span> <span class="o">:=</span> <span class="mi">1</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;initial:&quot;</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">zeroval</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;zeroval:&quot;</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>The <code>&amp;i</code> syntax gives the memory address of <code>i</code>,
i.e. a pointer to <code>i</code>.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">zeroptr</span><span class="p">(</span><span class="o">&amp;</span><span class="nx">i</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;zeroptr:&quot;</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Pointers can be printed too.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;pointer:&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="nx">i</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p><code>zeroval</code> doesn&rsquo;t change the <code>i</code> in <code>main</code>, but
<code>zeroptr</code> does because it has a reference to
the memory address for that variable.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="gp">$</span> go run pointers.go
<span class="go">initial: 1</span>
<span class="go">zeroval: 1</span>
<span class="go">zeroptr: 0</span>
<span class="go">pointer: 0x42131100</span>
</pre></div>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="structs">Structs</a>.
</p>
<p class="footer">
by <a href="https://twitter.com/mmcgrana">@mmcgrana</a> | <a href="mailto:mmcgrana@gmail.com">feedback</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/pointers">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
</body>
</html>