HOW TO – Setup your first Go Lang’s Workspace and run your 1st `Hello World` in your Linux box

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
download_golang
Go Lang’s download page
  • Once the download process is completed, open your terminal window, go to the directory where contains the downloaded go lang’s binary file (e.g. ~/Downloads)

downloaded_golang_binary

  • Run tar -xvf command to extract extract the content of go lang binary file.
extract_golang_bin
Running tar -xvf command for extracting go lang binary file.
  • Move the extracted folder go into /opt folder.

move_go_to_opt

  • Go to home directory then edit .profile file 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 GOROOT which points to the location of our moved go binary files, the /opt/go directory. Then, add a line which export & concat the $GOROOT/bin path with PATH variable so that we could run go in any directories.

add_goroot_in_profile

  • Save the changes in .profilefile then run source .profile command 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 should be able to run go command from any directories.

test_goroot

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 somewhere in your home directory (e.g. ~/projects/golang). This directory will be the root of our golang’s workspace directory. Within the directory, create 3 new sub directories with these following names: bin, pkg, and src

workspace_src_bin_pkg

  • Going into src folder, we’ll create a new subdirectory which represents our source control provider such as github.com. Then, we are going to the created new subdirectory, and create another new subdirectory. We name the new subdirectory same as  our source control provider account’s name (e.g. WendySanarwanto).

workspace_src_github

  • Go back to home directory, edit .profile file again in a text editor.  Add a new entry which export GOPATH environment variable. Ensure that the GOPATH variable points to the path of our workspace directory (e.g. ~/Documents/projects/golang ). Save changes and re-run source .profile command to force the changes taking effect in your environment immediately.

gopath_in_profile

 

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. ~/Documents/projects/golang/src/github.com/name/hello-golang )
  • Create a new .go file (e.g. hello.go ). Open the file using code editor such as Visual Studio Code.
  • Inside opened the blank .go file, we’ll write our 1st hello world in go lang as follow:

hello-golang

  • We’ll go back to terminal, and run go install github.com/username/hello-golang command to compile the program. The compiled binary file will be put under $GOPATH/bin directory.

golang_compiled_binary

  • Since we have exported the $GOPATH/bin as a part of $PATH variable, we should be able to run the program through running hello-golang command (name of your compiled go lang program).

run_golang_binary_file

 

Conclusion

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 GOPATH & 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.