GoLang (also referred to as Go) is an open-source statically typed programming language developed by Robert Griesemer, Rob Pike, and Ken Thompson at Google and launched in November 2009.
GoLang is known to be 3x faster than the python programming language and supports memory safety, garbage collection, structural typing, and CSP-style concurrency.
Popular applications like Monzo (banking app), Allegro (Ecommerce app), SoundCloud (Music app), and Uber (Ridesharing app) are using GoLang in their applications.
Stick with us to learn “how to install GoLang in Ubuntu Linux in 5 simple steps“.
Table of Contents
Step 1: GoLang Install
The best part about GoLang installation in Ubuntu is that you don’t have to run anywhere. Ubuntu or other Debian-based distributions include the GoLang repository making it available for direct installation from the default package manager (ex: apt).
Note: You may need the root privileges or sudo access to install it from apt package manager.
$ sudo apt install golang-go
Step 2: Creating Required Directories
After installation, we need to create a few important directories for GoLang to make it work properly. These directories are bin, src, and pkg as follows.
- bin: Contain Go Executable Binaries.
- src: Store your source files
- pkg: Store package objects
Execute the below command in the terminal to create all the directories as mentioned earlier.
$ mkdir -p ~/go/{bin,src,pkg}
Step 3: Add GoLang binary to the PATH environment variable
Few important environmental variables need to be added to make GoLang work properly. This step differs depending upon the user’s default shell. Use the below command to know your current shell.
$ echo $0
bash
Suppose you were using a bash shell, edit .bashrc or .bash_profile, and place the below content at the end of the file. For the zsh shell, edit the .zshrc file instead of any above.
export GOPATH="$HOME/go"
export GOBIN="$GOPATH/bin"
Step 4: Applying Changes
Changes may not be reflected directly until the next bootup in your system. Use the below command to apply all changes immediately depending upon the files you edited in step 3.
$ source ~/.bashrc
OR
$ source ~/.bash_profile
OR
$ source ~/.zshrc
Step 5: Test Go Command
Finally, we reach the last step; first, let’s verify the GoLang is correctly installed by checking whether the version is being displayed or not from the below command.
$ go version
go version go1.18.1 linux/amd64
We have done the installation part; if you are a beginner in Go Programming Language, follow the below methods to create your first application in GoLang.
Create and Run the first Go Program
First, we will have to create a new directory in src with the project name, “hello”, using the mkdir command as shown below.
$ mkdir -p $GOPATH/src/hello
Then choose your text editor to create a new hello.go file as shown below.
Note: Remember all your GoLang source file should be end with .go extension.
$ nano $GOPATH/src/hello/hello.go
Now add the below content inside this file to create a simple Go Program for displaying “Hello, TRENDOCEANS” as an output in your terminal.
package main
import "fmt"
func main() {
fmt.Printf("Hello, TRENDOCEANS\n")
}
Finally, compile and run the program as shown below.
$ go install $GOPATH/src/hello/hello.go
$ $GOBIN/hello
Hello, TRENDOCEANS
Remove GoLang from Ubuntu
If you don’t have any work with GoLang anymore and want to remove it from your Ubuntu or any other Debian-based distro, follow the below command to remove it from your Linux system.
$ sudo apt purge golang-go
$ sudo apt autoremove
$ sudo rm -rf $HOME/go
For any queries related to installation, complication, or package removal, please forward them in the comment section.
Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.