Table of contents
No headings in the article.
Go is an open source programming language used for general purpose. It's common purpose is its use as a backend programming language, containers such as Kubernetes are also built using Go. Even with its similarity to Python and the obvious popularity of Python, Go has some features that makes it more efficient.
Go programs are statically linked meaning when it is compiled, everything is included in a single binary executable and no external dependencies will be required that would need to be installed on the remote machine.
Go is a platform-independent language meaning binary executables can be produced for all the operating systems.
Go is a performant language and has fast compilation and fast run time with lower resource usage like CPU and memory especially compared to python.
Go has a standard library that has the majority of functionality that would be needed for DevOps built directly into it. This includes functionalities like file processing, HTTP web services, JSON processing, native support for concurrency and parallelism as well as built-in testing.
At the end of this article you will have go
installed on your system and you will be able to run and build your first go
code.
Nice To Know: The Distro being used in this article is Ubuntu 20.04
Step 1: Go to "golang.org" Click on Download and Download for Linux
Step 2: Open up your terminal and type:
wget https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz
-This command downloads the installer file, the tarball of Go files using the "wget" command.
Check the downloaded file using the
ls
command. Thels
command lists out all files and directorues in the current directory.If everything is working right, a tarball would be downloaded successfully and the file will contain
.tar.gz
extension which means it is agzipped tar
file.
Step 3: To extract the tarball using the tar command to a directory of our choice. For this example we will be extrating it to /usr/local/ for our convenience. In your terminal type:
` sudo tar -C /usr/local/ -xzf go1.13.5.linux-amd64.tar.gz `
The file has now been extracted to the directory /usr/local.
- The flag -C means navigate to the directory given and here it is /usr/local
The flags -xzf are three separate flags used in unison, -x for extract -z for gzip -f for file
To check if the extraction is successful,
cd /usr/local
_- In the directory, type inls
and if everything is right go willbe listed as the files in the directory meaning it has been extracted without any hassle.
Step 4: We have to set the $PATH variable for Go.
The $PATH
variable means that when a command is typed to run, the system looks for it in the directories specified by $PATH
in the order specified.
- To check our current
$PATH
, in the command line type:
` echo $PATH `
-To set the path for go we have to open the .profile file in our home directory
and append the $PATH
to that file and save it.
- To open the file in the command line with sudo privileges,
sudo nano $HOME/.profile
This will open the file in the terminal window
Append this command in the file
export PATH=$PATH:/usr/local/go/bin
Save the file and then run
source .profile
to apply the changes in the system.To confirm the file is saved in the properly, run
` cat $HOME/.profile `
Step 5: Run go version
This confirms that go has been installed and the current working version.
The next couple of steps will be about setting the path for go
Step 6: confirm the go
path by typing in the command line
go env
- Make the `go` directory by using the command `mkdir go` then `cd` into that directory
- In the newly created `go` directory, make three more directories:
- `mkdir bin` which will handle all your binaries
- `mkdir pkg` which will handle all your packages
- `mkdir src` where your code will reside
Step 7: This step will involve creating your first go
file
cd
into thesrc
directory and create a new foldermkdir HelloWorld
- In the directory, create your
go
file namedmain.go
- Copy and paste this code in it:
` package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
'
- This code will import the package
fmt
that's used for input and output ingo
and will use it to run the function called main that will print, "Hello, World"
Step 8: This step involves running and building your code.
- To run your code, on the commandline, go the path where your code is and run,
go run main.go
This will print out "Hello World'
To build your code, that is to make it a binary executable,
go build main.go
- This will create a
main
file that can be executed.
There you go, now you have go
installed in your machine and you have created, ran, and built your first go
code.
Congratulations!