Chapter 2: Setup Go Lang and Hello World
Note: All Chapters Index (In case the reader needs to refer something back)
Go setup before version 1.11
Before we begin we should be aware of the difference in Go Lang before and after version 1.11.
Before version 1.11 we have a few environment variables which define the location and setup of the Go project.
- GOROOT: It is a variable that defines where your Go SDK is located. You do not need to change this variable unless you plan to use different Go versions or install Go in a different directory than default (
/usr/local/go
) - GOPATH: It is the place where all Go projects should be located. For all
*.go
files to be able to locate and load other modules and dependencies they all must reside under this GOPATH. This means it defines the Go workspace and any new Go project should start within this workspace. But thankfully we don’t need it after version 1.11. But then how a Go project determines which place it should look into for Go project files? The answer is Go Modules, on a high level I would say now you can create a Go project anywhere and a file calledgo.mod
in each project will take care of all the dependencies installed and modules referred. - GOBIN: The directory where
go install
andgo get
will place binaries after buildingmain
packages (Go is compiled language). Generally, this is set to somewhere on the systemPATH
as$GOPATH/bin
by default or$HOME/go/bin
so that installed binaries can be run and discovered easily. We don’t have to set this explicitly in case you followed Chapter 1 or while creating binaries of project we can create them in same directory as project and refer the path to execute the binaries (I prefer later approach).
To get the value of all these env variables we have a command go env
It prints all of the env variables related to Go. In order to see only a few env variables, append the name of variables after the command.
$ go env GOBIN GOPATH GOROOT/home/himanshu/go
/usr/local/go
Note: I get these values from a Go project initialized with go mod init <project_name>
(which I explained later in this chapter) Here GOBIN is empty and the other two lines show GOPATH and GOROOT respectively.
I am considering that we are not working with old version of Go lang. Thus we will not dig further here about old setup and see how we setup Go workspace with recent versions.
Go setup after version 1.11
Now let's begin with the setup of our Go lang project using Go Modules. This will be the standard way to set up any Go project.
- Create a directory anywhere for the project and move inside.
mkdir hello_world
cd hello_world
- Initialize a project with command
go mod init <project_name>
$ go mod init hello_world
go: creating new go.mod: module hello_world
- A
go.mod
file is created and which contains the project name and version of Go this project is using. We are making this project for demo purpose only, But a more matured project should have module name asyour_domain/account_name/repo_name
in project name (e.g.github.com/user_name/hello_world
). You can also refer this official Gorilla Mux Project and it’sgo.mod
file. - Make a
hello.go
file and write below code in it
- Here
package main
is the line which declare that file belong tomain
package.main
package is mandatory to have in a Go Project because it tells the Go compiler that the package should compile as an executable program instead of a shared library.main
package act as entry point in Go Project. In main package there should be one (and only one) file which belong tomain
package and havemain()
method in it. Commonly it is named asmain.go
but here we named ithello.go
Thismain()
method act as entry point for execution of program and executed first. - Run the file using
go run hello.go
and you can see the output (Hello World). - But we instead of just executing the file this way we can compile it first and then run. This is the how we do it in production, we compile the project into binaries and then send it into production to execute. To do this we will use
go build hello.go
command to build the binaries and./hello
to run the binaryhello
.
Congratulations !! on writing your first Go program. Keep going.
Read the comprehensive Go Lang course Here.
Feel free to reach Himanshu Patel for any doubt or help needed, contact info in the about section.