• Home
  • ES6
  • ADVANCED
    • Javascript Array Methods
    • Javascript String Methods
    • Javascript Regex
    • ES Next
  • JS BOM
  • JS DOM
  • WEB API
  • SNIPPETS
  • TYPESCRIPT
  • Golang
  • Dark Mode Light Mode
  • » golang » Go Syntax
    <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">&quot;fmt&quot;</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">&quot;Hello World!&quot;</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 (&quot;fmt&quot;)</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 &quot;Hello World!&quot;.</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(&quot;Hello World!&quot;)</code> is a statement.</p> <p>In Go, statements are separated by ending a line (hitting the Enter key) or by a semicolon &quot;<code>;</code>&quot;.</p> <p>Hitting the Enter key adds &quot;<code>;</code>&quot; 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">&quot;fmt&quot;</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">&quot;Hello World!&quot;</span>);} </code></pre>

    Go(Golang) Tutorial

    Go Introduction

  • What is Go?
  • Go Get Started
  • Go Syntax
  • Go Variables
  • Go Arrays
  • About Me

    • Made by Rasel Mahmud
    • Portfolio

    Top References

    • javascripttutorial.net
    • W3school.com

    Top Tutorials

    • Primitive vs. Reference Values
    • What is JavaScript

    NOTE:

    This website only for Developing purpose not for business. and all content copied from other place like javascripttutorial.net and W3school.com