This blog provides information on first steps to getting started with Go Language.
We start with Installation of Go, then understand the concepts of GOPATH and Workspaces in Go, and then look at some basic commands of Go Lang.
This blog is part of the tutorial series – Learn Go, in which we will learn about Go language step by step.
Table of Contents
1.0 Go Installation
Installation is very simple.
Just download the installer from Go Lang Website, and following the installation instructions for your operating system.
I am on Mac, so for me installation involved just 2 steps
- Downloading the Package file. e.g go1.11.2.darwin-amd64.pkg
- Run the installer and follow the steps
- Go is installed to folder
- The following folder –
/usr/local/go/bin, is also added to
PATHenvironment variable of the system
- Go is installed to folder
- Restart all open terminals, after installation
To test if installation was successful, run below command in
If installed correctly you may see some output like this below
go version go1.11.1 darwin/amd64
2.0 GOROOT / GOPATH / Workspace
- This is the folder where Go is installed – The Root Folder
- Typically on Mac OS, it will be
- You can check your GOROOT folder by running below command
➜ ~ go env GOROOT /usr/local/go
- This is the folder that contains your Go Workspace (folder where you put all your source code)
gofolder inside your home folder. e.g
$HOME/goon Mac OS.
- You can check the
GOPATHby running below command
➜ ~ go env GOPATH /Users/in-sunit.chatterjee/go
- You can also set your
GOPATHto any other folder also.
- Workspace is a folder that contains the Go Source Code.
- This is what a typical workspace looks like
It contains two folders generally
- contains all the executables for the projects
- Contains the Go Source Code
- It may contain may source control repositories (e.g Git repositories)
- Repository contains one or more Go Packages
- Each package contains the Go source file.
For e.g. this is how my current workspace folder looks like.
go ├── bin │ ├── example │ ├── hello │ ├── guru │ └── tour │ └── src ├── LearningGo │ └── examples │ ├── concurrency │ │ ├── GoRoutines_1.go │ ├── encapsulation │ │ ├── FunctionRecievers.go │ ├── errorhandling │ │ └── HandlingErrors.go │ └── polymorphism │ └── InterfacesExample.go ├── github.com │ ├── acroca │ │ └── go-symbols │ │ └── main.go │ ├── brianvoe │ │ └── gofakeit │ │
Typically Go Programmers keep their entire code in a single workspace.
This may seem a little wierd and constraining at start, because we are used to putting our source code anywhere we like.
The reason we have to do so in GO is because it searches for the imported packages relative to the
GOPATHfolder. If your source code is in any other folder, then the package imports may not work.
3.0 Basic Go Commands
Before we go further, we assume you have some familarity with shell commands like
cat, etc, as we will be using them in our examples.
3.1 Building Go Source Code
- Run command –
- File Name is optional
- Example – Building a single file.
As you can see that after you build a Go file, an executable file with same name is created in the same folder.
- Example – Building without file name
We can also build without the file name. It will build the code in current package.
3.2 Formatting a Go Source File
- Can be done using command –
- Formats the go source code in the file
- Example – See contents of hello.go before and after formatting
3.3 Executing a Go Program
We can execute a Go Program by command –
go run [filename]
➜ go run hello.go hello, world
3.4 Fetching Remote Packages
There are time when we need to import remote packages for use in our program.
go get command downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like
- We want to use library in – https://github.com/tatsushid/go-fastping
- We can do it using following command
go get -u https://github.com/tatsushid/go-fastping
3.5 Cleaning build / test / object / temporary files, etc
In the next blog of the Learn Go tutorial series, we will look at Data types and Variables in Go