Getting started with the Golang
In this class, we’ll cover the basics that are necessary to get you started with Golang. By the end of this class, you should be in a position to set up your desktop environment ready to begin coding using Golang.
Before we start off, it’s important to note that Golang is an open-source programming language. Therefore, it doesn’t matter whether you’re using Linux OS, Mac OS or Windows OS. So, let’s dive in and find out how you can install and run Golang programs in Linux OS, Mac OS or Windows OS.
How to Install Golang on Linux OS
Whether you’re using a Debian based Linux distribution—such as Ubuntu or Linux Mint—or CentOS/RHEL based distribution such as Fedora, it’s pretty simple to install Golang.
1.For Debian based Linux distributions, follow the steps below:
-
- • Open your terminal
-
- • Type the following command at the command prompt: “sudo apt-get install golang”
-
- • Enter your root password
- • Wait for the installation process to complete
2.For CentOS/RHEL based distribution such as Fedora follows the steps below:
-
- • Open your terminal
-
- • Type the following command at the command prompt: “sudo yum install golang”
-
- • Enter your root password
- • Wait for the installation process to complete
Note that you can check the version of your Golang by typing the following command at the command prompt: go version. The latest version if go 1.5.2.
How to install Golang on Mac OS
Here’s how you can install Golang on your Mac OS:
-
- • Download the Golang package file from this link.
-
- • Open your package and follow the prompts to install the Golang on your computer.
-
- • Your path environment variable for your Golang should be set to “/usr/local/go/bin” directory.
- • Restart your computer so that the changes you’ve made takes effect.
How to install Golang on Windows OS
Two options are available if you want to install Golang on your Windows OS computer. You can either download and install the “.zip” file or download and install the “.msi” file.
1.For “.zip” files, here’s how you can install Golang your Windows OS:
-
- • Download the zip file from this link and extract it to the folder of your choice. For instance, you can create a Go folder in Local disk C:
-
- • Set your GOROOT environment variable for the chosen path. This should be “C:\Go” in your case.
- • Add the bin subdirectory of your GOROOT. In this case, your path environment variable will be “\Go\bin.”
2.For “.msi” files, here’s how you can install Golang your Windows OS:
-
- • Download the Golang package file from this link.
-
- • Open your package and follow the prompts to install the Golang on your computer.
-
- • Your path environment variable for your Golang should be set to “c:\Go\bin” directory
- • Restart your computer so that the changes you’ve made takes effect.
An Overview of Golang Application
Now that you’ve installed the Golang on your computer, what next? You should begin writing your Golang codes.
But first, you should understand how the Golang applications are structured.
All your Golang apps will be organized around workspaces. You can look at a workspace as directory hierarchy that contains three directories at its root. These are:
-
- • The “src”. It contains the Golang source code files that have been organized into packages.
-
- • The “pkg”. It contains the package objects—they act like namespaces. Packages can either be used from a standard library, or you can create your packages.
- • The “bin”. It has the executable commands.
The location of your workspace is specified by the GOPATH environment variable. The packages that are used from the Golang standard library are given short paths such as the “fmt” or the “net/http”. However, if you create your packages, you should select a base path shouldn’t collide with future additions to your standard library.
So, how can you create your first application? Good question.
Here’s is how you can successfully create and run a simple application in Golang:
-
- • Select the package path and create a corresponding package directory that will be inside your workspace.
-
- • Create a file and save it using “.go” file extension inside the directory that you’ve created.
-
- • Type you code. You can use the example below:
package main import "fmt" func main () { fmt.Printf ("Hello, I’m beginning to code in Golang \n") }
• Build and run you program using the go tool.
So, there you have it—you’ve learnt how to set up the Golang environment and how to create your first application.
Next up, we look at more about Golang environments.
GET YOUR FREE Golang EBOOK!