<p>Variables are containers for storing data values.</p>
<hr>
<h2 id="go-variable-types">Go Variable Types</h2>
<p>In Go, there are different <strong>types</strong> of variables, for example:</p>
<ul>
<li><code>int</code>- stores integers (whole numbers), such as 123 or -123</li>
<li><code>float32</code>- stores floating point numbers, with decimals, such as 19.99 or -19.99</li>
<li><code>string</code> - stores text, such as "Hello World". String values are surrounded by double quotes</li>
<li><code>bool</code>- stores values with two states: true or false</li>
</ul>
<p>More about different variable types, will be explained in the <a href="https://www.w3schools.com/go/go_data_types.php">Go Data Types</a> chapter.</p>
<hr>
<h2 id="declaring-creating-variables">Declaring (Creating) Variables</h2>
<p>In Go, there are two ways to declare a variable:</p>
<h4 id="1-with-the-var-keyword">1. With the <code>var</code> keyword:</h4>
<p>Use the <code>var</code> keyword, followed by variable name and type:</p>
<h3 id="syntax">Syntax</h3>
<pre><code class="hljs language-go"><span class="hljs-keyword">var</span> variablename <span class="hljs-keyword">type</span> = value
</code></pre>
<p><strong>Note:</strong> You always have to specify either <code>type</code> or <code>value</code> (or both).</p>
<h4 id="2-with-the--sign">2. With the <code>:=</code> sign:</h4>
<p>Use the <code>:=</code> sign, followed by the variable value:</p>
<h3 id="syntax-1">Syntax</h3>
<p><em>variablename</em> := <em>value</em></p>
<p><strong>Note:</strong> In this case, the type of the variable is <strong>inferred</strong> from the value (means that the compiler decides the type of the variable, based on the value).</p>
<p><strong>Note:</strong> It is not possible to declare a variable using <code>:=</code>, without assigning a value to it.</p>
<hr>
<h2 id="variable-declaration-with-initial-value">Variable Declaration With Initial Value</h2>
<p>If the value of a variable is known from the start, you can declare the variable and assign a value to it on one line:</p>
<h3 id="example">Example</h3>
<pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main
<span class="hljs-keyword">import</span> (<span class="hljs-string">"fmt"</span>)
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
<span class="hljs-keyword">var</span> student1 <span class="hljs-type">string</span> = <span class="hljs-string">"John"</span> *<span class="hljs-comment">//type is string*</span>
<span class="hljs-keyword">var</span> student2 = <span class="hljs-string">"Jane"</span> *<span class="hljs-comment">//type is inferred*</span>
x := <span class="hljs-number">2</span> *<span class="hljs-comment">//type is inferred*</span>
fmt.Println(student1)
fmt.Println(student2)
fmt.Println(x)
}
</code></pre>
<p><strong>Note:</strong> The variable types of <code>student2</code> and <code>x</code> is <strong>inferred</strong> from their values.</p>
<hr>
<hr>
<h2 id="variable-declaration-without-initial-value">Variable Declaration Without Initial Value</h2>
<p>In Go, all variables are initialized. So, if you declare a variable without an initial value, its value will be set to the default value of its type:</p>
<h3 id="example-1">Example</h3>
<pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main
<span class="hljs-keyword">import</span> (<span class="hljs-string">"fmt"</span>)
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
<span class="hljs-keyword">var</span> a <span class="hljs-type">string</span>
<span class="hljs-keyword">var</span> b <span class="hljs-type">int</span>
<span class="hljs-keyword">var</span> c <span class="hljs-type">bool</span>
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
}
</code></pre>
<h4 id="example-explained">Example explained</h4>
<p>In this example there are 3 variables:</p>
<ul>
<li><code>a</code></li>
<li><code>b</code></li>
<li><code>c</code></li>
</ul>
<p>These variables are declared but they have not been assigned initial values.</p>
<p>By running the code, we can see that they already have the default values of their respective types:</p>
<ul>
<li><code>a</code> is <code>""</code></li>
<li><code>b</code> is <code>0</code></li>
<li><code>c</code> is <code>false</code></li>
</ul>
<hr>
<h2 id="value-assignment-after-declaration">Value Assignment After Declaration</h2>
<p>It is possible to assign a value to a variable after it is declared. This is helpful for cases the value is not initially known.</p>
<h3 id="example-2">Example</h3>
<pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main
<span class="hljs-keyword">import</span> (<span class="hljs-string">"fmt"</span>)
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
<span class="hljs-keyword">var</span> student1 <span class="hljs-type">string</span>
student1 = <span class="hljs-string">"John"</span>
fmt.Println(student1)
}
</code></pre>
<p><strong>Note:</strong> It is not possible to declare a variable using "<code>:=</code>" without assigning a value to it.</p>
<hr>
<h2 id="difference-between-var-and-">Difference Between var and :=</h2>
<p>There are some small differences between the <code>var</code> var <code>:=</code>:</p>
<table>
<thead>
<tr>
<th align="left">var</th>
<th align="left">:=</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Can be used <strong>inside</strong> and <strong>outside</strong> of functions</td>
<td align="left">Can only be used <strong>inside</strong> functions</td>
</tr>
<tr>
<td align="left">Variable declaration and value assignment <strong>can be done separately</strong></td>
<td align="left">Variable declaration and value assignment <strong>cannot be done separately</strong> (must be done in the same line)</td>
</tr>
</tbody></table>
<h3 id="example-3">Example</h3>
<p>This example shows declaring variables outside of a function, with the <code>var</code> keyword:</p>
<pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main
<span class="hljs-keyword">import</span> (<span class="hljs-string">"fmt"</span>)
<span class="hljs-keyword">var</span> a <span class="hljs-type">int</span>
<span class="hljs-keyword">var</span> b <span class="hljs-type">int</span> = <span class="hljs-number">2</span>
<span class="hljs-keyword">var</span> c = <span class="hljs-number">3</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
a = <span class="hljs-number">1</span>
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
}
</code></pre>