<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Cali Tabilas</title>
    <description>The latest articles on Forem by Cali Tabilas (@cali3192).</description>
    <link>https://forem.com/cali3192</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F327821%2F8d1d553a-888a-49e4-b6c5-8584eec7078c.png</url>
      <title>Forem: Cali Tabilas</title>
      <link>https://forem.com/cali3192</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cali3192"/>
    <language>en</language>
    <item>
      <title>Learning Go from JavaScript</title>
      <dc:creator>Cali Tabilas</dc:creator>
      <pubDate>Mon, 22 Mar 2021 14:47:24 +0000</pubDate>
      <link>https://forem.com/cali3192/learning-go-from-javascript-4n6e</link>
      <guid>https://forem.com/cali3192/learning-go-from-javascript-4n6e</guid>
      <description>&lt;p&gt;This is will be a series quick, little blogs to document what it is like learning Go from a JavaScript background. I hope to keep each post &amp;lt;5min. I'll try to release posts on a weekly/biweekly cadence to keep me accountable on this journey. &lt;/p&gt;

&lt;p&gt;One of the reasons why I wanted to learn Go is because I work with enterprise TypeScript/JavaScript aka  flexible languages with Dynamic types. I wanted to challenge myself to work with a statically typed language so that different checks occur at compile vs run time. Another interesting distinction between JavaScript and Go is that Go has a smaller memory footprint and provides the convenience of statically linked binaries.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Getting Started&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To download Golang, visit their homepage &lt;a href="https://golang.org/doc/install"&gt;here&lt;/a&gt; and simply follow the installation instructions for your machine. I personally use VSCode as my primary editor to run Go. I first had to add the golang.go extension to get language support. As of today, this extension has 4.5M downloads. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://golang.org/doc/install"&gt;Download and install&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Directory Structure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;For the purpose of creating a Hello World project, we will just need to create a root directory and a main file with the &lt;code&gt;.go&lt;/code&gt; file extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;touch&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;main.go&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nx"&gt;main.go&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;File Structure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The primary requirement for a Go program is that it must have a main function as part of the main package. This main function serves as an entry point for executing our go file. &lt;/p&gt;

&lt;p&gt;In its simplest form, there are 3 components to a Go file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Executable - Your file will either be executable or a non-executable utility.

&lt;ul&gt;
&lt;li&gt;The main package will contain a main function that denotes the start of a program.&lt;/li&gt;
&lt;li&gt;Any package other than main package is a non-executable package and it is not self-executable. Can also be called anything other than main.&lt;/li&gt;
&lt;li&gt;Non-executables more or less serve as utility functions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Imports - Import statements let us bring in external libraries that we can leverage in our program. Some useful libraries for starting out:

&lt;ul&gt;
&lt;li&gt;fmt&lt;/li&gt;
&lt;li&gt;math&lt;/li&gt;
&lt;li&gt;time&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Main - Entry point and Go looks to execute this main function
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// main.go&lt;/span&gt;

&lt;span class="c"&gt;// 1. executable files will be called main&lt;/span&gt;
&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="c"&gt;// 2. imports to be declared before main &lt;/span&gt;
&lt;span class="c"&gt;// bringing fmt to print &lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="c"&gt;// functions can be defined outside main&lt;/span&gt;
&lt;span class="c"&gt;// in this case, defining a simple helloWorld&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;helloWorld&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// 3. define main function and Go looks to execute this funct&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// passing an input to the helloWorld func&lt;/span&gt;
    &lt;span class="n"&gt;helloWorld&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Golang"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Running the Program&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Running Go is as simple as using the &lt;code&gt;go run&lt;/code&gt;  command and pointing to your go file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;go run main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the above command, we will see the following in the terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Hello, Golang
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 An executable program in Go can be created using go build. The file will look like a series of binaries.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Alternatively, we can create a &lt;strong&gt;binary executable file&lt;/strong&gt;, you can use the &lt;code&gt;go build&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;go build main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This executable file can be found in the current directory. You will likely get a warning if you try to open this new main file because it is binary. Our new directory will now look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nx"&gt;main.go&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;this&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;is&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;executable&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Next Blog:
&lt;/h3&gt;

&lt;p&gt;I will be exploring the basic Go data structures, the differences in variable swapping with Go vs JavaScript, and some basic Go operators.&lt;/p&gt;

&lt;h2&gt;
  
  
  Helpful Resources
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Go Tour
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;a href="https://tour.golang.org/basics/"&gt;Go Tour&lt;/a&gt;&lt;/strong&gt; is extremely useful in learning the basics of Go from the site itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Effective Go&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Similar to Effective TypeScript, the &lt;strong&gt;&lt;a href="https://golang.org/doc/effective_go"&gt;Effective Go&lt;/a&gt;&lt;/strong&gt; highlights best practices and how to leverage the best parts of Go. &lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>beginners</category>
      <category>go</category>
    </item>
  </channel>
</rss>
