gobyexample/public/pointers
2022-12-24 19:49:13 +01:00

239 lines
8.9 KiB
Plaintext
Generated

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Pointers</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'recursion';
}
if (e.key == "ArrowRight") {
window.location.href = 'strings-and-runes';
}
}
</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="https://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="https://go.dev/play/p/OlWCLpxAyBz"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="s">&#34;fmt&#34;</span>
</pre>
</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">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">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>
</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">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">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>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">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="nf">Println</span><span class="p">(</span><span class="s">&#34;initial:&#34;</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nf">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="nf">Println</span><span class="p">(</span><span class="s">&#34;zeroval:&#34;</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre>
</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">
<pre class="chroma">
<span class="nf">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="nf">Println</span><span class="p">(</span><span class="s">&#34;zeroptr:&#34;</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>Pointers can be printed too.</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;pointer:&#34;</span><span class="p">,</span> <span class="o">&amp;</span><span class="nx">i</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</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">
<pre class="chroma">
<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>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="strings-and-runes">Strings and Runes</a>.
</p>
<p class="footer">
<script>
window.onload = function() {
function styleAllTags(tag, style, value){
document.querySelectorAll(tag).forEach((tag) => {
tag.style[style] = value
})
}
var themeButton = document.getElementById('theme-button');
var body = document.getElementsByTagName('body')[0];
function activateTheme(theme) {
if (theme === 'dark') {
body.style.color = "#ffffffeb";
body.style.backgroundColor = "#1a202c";
styleAllTags('a', 'color', '#ffffffeb')
styleAllTags('td.code', 'background', '#333333')
styleAllTags('td.code.empty', 'background', '#1a202c')
} else {
body.style.color = "#252519";
body.style.backgroundColor = "white";
styleAllTags('a', 'color', '#252519')
styleAllTags('td.code', 'background', '#f0f0f0')
styleAllTags('td.code.empty', 'background', 'white')
}
localStorage.setItem('theme', theme);
}
var currentTheme = localStorage.getItem('theme');
if (currentTheme === 'light' || currentTheme === 'dark') {
activateTheme(currentTheme);
} else {
activateTheme('light');
}
themeButton.onclick = function() {
var newTheme = localStorage.getItem('theme') === 'dark' ? 'light' : 'dark';
activateTheme(newTheme);
}
}
</script>
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> and <a href="https://eli.thegreenplace.net">Eli Bendersky</a> | <a href="https://github.com/mmcgrana/gobyexample">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> | <button id="theme-button">Toggle theme</button>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func zeroval(ival int) {\u000A ival \u003D 0\u000A}\u000A');codeLines.push('func zeroptr(iptr *int) {\u000A *iptr \u003D 0\u000A}\u000A');codeLines.push('func main() {\u000A i :\u003D 1\u000A fmt.Println(\"initial:\", i)\u000A');codeLines.push(' zeroval(i)\u000A fmt.Println(\"zeroval:\", i)\u000A');codeLines.push(' zeroptr(\u0026i)\u000A fmt.Println(\"zeroptr:\", i)\u000A');codeLines.push(' fmt.Println(\"pointer:\", \u0026i)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>