This article covers brief steps of how to install, setup necessary tools which could help you to write & run your 1st Go Lang program, in your Linux machine.
Download & Installing Go
- Browse to https://golang.org/dl/ and click the download link of Go Lang’s Linux Binary
- Once the download process is completed, open your terminal window, go to the directory where contains your the downloaded go lang’s binary file (e.g.
tar -xvfcommand to extract extract the content of go lang binary file.
- Move the extracted folder
- Go to home directory then edit
.profilefile in there. In case you have not created it yet, create a new one. Open the file in text editor such as vim, and write down a line for declaring a new environment variable
GOROOTwhich points to the location of our moved go binary files, the
/opt/godirectory. Then, add a line which export & concat the
PATHvariable so that we could run
goin any directories.
- Save the changes in
.profilefile then run
source .profilecommand in terminal to get our new changes take effect immediately in our environment.By the next time you login or booting into your linux box using your current login account, you would be able to run
gocommand from any directories.
Setup your 1st Go Lang workspace
Workspace in Go Lang, is a directory which contains source code of our go lang application & library projects, 3rd party go dependencies & binary files of our compiled go lang projects. Below are steps of how to create it:
- Create a new directory in somewhere in your home directory (e.g. ~/projects/golang). This directory will be the root our golang’s workspace directory. Within the directory, create 3 new sub directories with these following names:
pkg, and src
- Going into
srcfolder, we’ll create a subdirectory which represent of our source control provider such github.com. Then going into the created subdirectory, we’ll create another subdirectory. We name it with our source control provider account’s name.
- Go back to home directory, edit
.profilefile again in a text editor. Add a new entry which export
GOPATHenvironment variable. Ensure that the
GOPATHvariable points to the path of our workspace directory (e.g.
~/Documents/projects/golang). Save changes and re-run
source .profilecommand to force the changes taking effect in your environment immediately.
Creating your 1st “Hello World” Go Lang project
From here, we have already created our initial go lang workspace. Now, we are ready to create our 1st “Hello World” Go Lang project.
- Go to the workspace’s source code directory then create a new directory (e.g.
- Create a new
hello.go). Open the file using code editor such as Visual Studio Code.
- Inside opened the blank
.gofile, we’ll write our 1st
hello worldin go lang as follow:
- We’ll go back to terminal, and run
go install github.com/username/hello-golangcommand to compile the program. The compiled binary file will be put under
- Since we have exported the
$GOPATH/binas a part of
$PATHvariable, we should be able to run the program through running
hello-golangcommand (name of your compiled go lang program).
At this point, we have already setup Go Lang workspace in our linux box. The workspace is a single location of where we will put files , directories of our current & future go lang source code projects, dependencies & compiled projects inside.
We also has setup
GOROOT environment variables, integrate them with the
PATH variable. This would make our efforts easier when we want to go into the workspace’s location, execute compiled binaries or just running
go command from any directories.