Go (also referred to as GoLang) is an open-source and lower-level programming language designed to enable users to easily write simple, reliable, and highly efficient computer programs.
Developed in 2007 at Google by a team of programmers – Robert Griesemer, Rob Pike, and Ken Thompson, it is a compiled, statically typed language same to other system languages such as C, C++, Java, and many more.
GoLang is highly productive, and readable with support for networking and multiprocessing and it is scalable in extensive systems as well. Below is a list of a few well known open source projects developed using GoLang:
- Docker
- Kubernetes
- Lime
- InfluxDB
- Gogs (Go Git Service) among others.
Install GoLang in Linux Systems
1. Go to https://golang.org/dl/ and download the latest version (i.e 1.15.2) of GoLang in an archive file using wget command as follows:
$ wget -c https://golang.org/dl/go1.15.2.linux-amd64.tar.gz [64-bit] $ wget -c https://golang.org/dl/go1.15.2.linux-386.tar.gz [32-bit]
2. Next, check the integrity of the tarball by verifying the SHA256 checksum of the archive file using the shasum command as below, where the flag -a
is used to specify the algorithm to be used:
$ shasum -a 256 go1.7.3.linux-amd64.tar.gz b49fda1ca29a1946d6bb2a5a6982cf07ccd2aba849289508ee0f9918f6bb4552 go1.15.2.linux-amd64.tar.gz
Important: To show that the contents of the downloaded archive file are the exact copy provided on the GoLang website, the 256-bit hash value generated from the command above as seen in the output should be the same as that provided along with the download link.
If that is the case, proceed to the next step, otherwise, download a new tarball and run the check again.
3. Then extract the tar archive files into /usr/local directory using the command below.
$ sudo tar -C /usr/local -xvzf go1.15.2.linux-amd64.tar.gz
Where, -C
specifies the destination directory..
Configuring GoLang Environment in Linux
4. First, set up your Go workspace by creating a directory ~/go_projects
which is the root of your workspace. The workspace is made of three directories namely:
bin
which will contain Go executable binaries.src
which will store your source files andpkg
which will store package objects.
Therefore create the above directory tree as follows:
$ mkdir -p ~/go_projects/{bin,src,pkg} $ cd ~/go_projects $ ls
5. Now it’s time to execute Go like the rest of Linux programs without specifying its absolute path, its installation directory must be stored as one of the values of $PATH environment variable.
Now, add /usr/local/go/bin
to the PATH environment variable by inserting the line below in your /etc/profile file for a system-wide installation or $HOME/.profile or $HOME./bash_profile for user-specific installation:
Using your preferred editor, open the appropriate user profile file as per your distribution and add the line below, save the file, and exit:
export PATH=$PATH:/usr/local/go/bin
6. Then, set the values of GOPATH
and GOBIN
Go environment variables in your user profile file (~/.profile or ~/bash_profile
) to point to your workspace directory.
export GOPATH="$HOME/go_projects" export GOBIN="$GOPATH/bin"
Note: If you installed GoLang in a custom directory other than the default (/usr/local/), you must specify that directory as the value of the GOROOT variable.
For instance, if you have installed GoLang in the home directory, add the lines below to your $HOME/.profile or $HOME/.bash_profile file.
export GOROOT=$HOME/go export PATH=$PATH:$GOROOT/bin
7. The final step under this section is to effect the changes made to the user profile in the current bash session like so:
$ source ~/.bash_profile OR $ source ~/.profile
Verify GoLang Installation
8. Run the commands below to view your Go version and environment:
$ go version $ go env
Type the following command to display usage information for the Go tool, which manages Go source code:
$ go help
9. To test your if your Go installation is working correctly, write a small Go hello world program, save the file in ~/go_projects/src/hello/ directory. All your GoLang source files must end with the .go
extension.
Begin by creating the hello project directory under ~/go_projects/src/:
$ mkdir -p ~/go_projects/src/hello
Then use your favorite editor to create the hello.go
file:
$ vi ~/go_projects/src/hello/hello.go
Add the lines below in the file, save it, and exit:
package main import "fmt" func main() { fmt.Printf("Hello, you have successfully installed GoLang in Linux\n") }
10. Now, compile the program above as using go install and run it:
$ go install $GOPATH/src/hello/hello.go $ $GOBIN/hello
If you see the output showing you the message in the program file, then your installation is working correctly.
11. To run your Go binary executables like other Linux commands, add $GOBIN to your $PATH environment variable.
Reference Links: https://golang.org/
That’s it! You can now go on and learn GoLang for writing simple, reliable, and highly efficient computer programs. Are you already make use of GoLang?
Share your experience with us and many other Linux users out there via the comment section below or imaginably, you can ask a question in relation to this guide or GoLang.
If you export
PATH=$PATH:$GOBIN
as well then you could just call hello instead of $GOBIN/hello.Just a suggestion but maybe that’s not the proper way to do things… newer Linux user here!
Thank you for the tip, I just did this myself.
I would like to add, to those noobs such as myself. If you do this tip after all the other steps in the article and it is saying no command when you run “hello” make sure you do the source command on the file where you added the path env variables like
If you don’t do this, it won’t work. (took me a couple of min of it not working to remember about the source command ran earlier.)
It would be nice to update this article to a more recent version of Go.
@GoUpdate,
We’ve updated the article to the most recent version of GoLang…
Under part 3:
“Where,
-C
specifies the destination directory..”Destination directory of what exactly?
@GWS
The destination directory is
/usr/local
as specified in the command.There’s a typo in your panel image itself. It’s “Google’s first programming language” and not “first Google’s programming language”
Thanks a lot for mentioning that, we shall correct it as soon as possible.