<h2 id="go-syntax">Go Syntax</h2>
<p>A Go file consists of the following parts:</p>
<ul>
<li>Package declaration</li>
<li>Import packages</li>
<li>Functions</li>
<li>Statements and expressions</li>
</ul>
<p>Look at the following code, to understand it better:</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-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
fmt.Println(<span class="hljs-string">"Hello World!"</span>)
}
</code></pre>
<h3 id="example-explained">Example explained</h3>
<p><strong>Line 1:</strong> In Go, every program is part of a package. We define this using the <code>package</code> keyword. In this example, the program belongs to the <code>main</code> package.</p>
<p><strong>Line 2:</strong> <code>import ("fmt")</code> lets us import files included in the <code>fmt</code> package.</p>
<p><strong>Line 3:</strong> A blank line. Go ignores white space. Having white spaces in code makes it more readable.</p>
<p><strong>Line 4:</strong> <code>func main() {}</code> is a function. Any code inside its curly brackets <code>{}</code> will be executed.</p>
<p><strong>Line 5:</strong> <code>fmt.Println()</code> is a function made available from the <code>fmt</code> package. It is used to output/print text. In our example it will output "Hello World!".</p>
<p><strong>Note:</strong> In Go, any executable code belongs to the <code>main</code> package.</p>
<h2 id="go-statements">Go Statements</h2>
<p><code>fmt.Println("Hello World!")</code> is a statement.</p>
<p>In Go, statements are separated by ending a line (hitting the Enter key) or by a semicolon "<code>;</code>".</p>
<p>Hitting the Enter key adds "<code>;</code>" to the end of the line implicitly (does not show up in the source code).</p>
<p>The left curly bracket <code>{</code> cannot come at the start of a line.</p>
<p>Run the following code and see what happens:</p>
<h2 id="go-compact-code">Go Compact Code</h2>
<p>You can write more compact code, like shown below (this is not recommended because it makes the code more difficult to read):</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> { fmt.Println(<span class="hljs-string">"Hello World!"</span>);}
</code></pre>