gobyexample/public/variables
Oleg Butuzov 646c8b39da bugfix (mmcgrana/gobyexample#294) sha1 sum calculator
This bugfix implements correct way to calculate source sha1 hash, before it will changed,
by propagation of unchanged sources.

This commit will also include regenerated static files and *.hash files.
2019-10-22 14:27:02 +03:00

191 lines
6.3 KiB
Plaintext
Generated

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Variables</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'values';
}
if (e.key == "ArrowRight") {
window.location.href = 'constants';
}
}
</script>
<body>
<div class="example" id="variables">
<h2><a href="./">Go by Example</a>: Variables</h2>
<table>
<tr>
<td class="docs">
<p>In Go, <em>variables</em> are explicitly declared and used by
the compiler to e.g. check type-correctness of function
calls.</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/iYyAIilyBRf"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<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">
</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>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p><code>var</code> declares 1 or more variables.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="kd">var</span> <span class="nx">a</span> <span class="p">=</span> <span class="s">&quot;initial&quot;</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>You can declare multiple variables at once.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="kd">var</span> <span class="nx">b</span><span class="p">,</span> <span class="nx">c</span> <span class="kt">int</span> <span class="p">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">b</span><span class="p">,</span> <span class="nx">c</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Go will infer the type of initialized variables.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="kd">var</span> <span class="nx">d</span> <span class="p">=</span> <span class="kc">true</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">d</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Variables declared without a corresponding
initialization are <em>zero-valued</em>. For example, the
zero value for an <code>int</code> is <code>0</code>.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="kd">var</span> <span class="nx">e</span> <span class="kt">int</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>The <code>:=</code> syntax is shorthand for declaring and
initializing a variable, e.g. for
<code>var f string = &quot;apple&quot;</code> in this case.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">f</span> <span class="o">:=</span> <span class="s">&quot;apple&quot;</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code">
<div class="highlight"><pre><span class="gp">$</span> go run variables.go
<span class="go">initial</span>
<span class="go">1 2</span>
<span class="go">true</span>
<span class="go">0</span>
<span class="go">apple</span>
</pre></div>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="constants">Constants</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/variables">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var a = \"initial\"\u000A fmt.Println(a)\u000A');codeLines.push(' var b, c int = 1, 2\u000A fmt.Println(b, c)\u000A');codeLines.push(' var d = true\u000A fmt.Println(d)\u000A');codeLines.push(' var e int\u000A fmt.Println(e)\u000A');codeLines.push(' f := \"apple\"\u000A fmt.Println(f)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>