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

  1. Downloading the Package file. e.g go1.11.2.darwin-amd64.pkg
  2. Run the installer and follow the steps
    1. Go is installed to folder /usr/local/go
    2. The following folder –/usr/local/go/bin, is also added to PATH environment variable of the system
  3. Restart all open terminals, after installation

To test if installation was successful, run below command in Terminal

go version

If installed correctly you may see some output like this below

go version go1.11.1 darwin/amd64

Top ∆

 

2.0 GOROOT / GOPATH / Workspace

2.1 GOROOT

  • This is the folder where Go is installed – The Root Folder
  • Typically on Mac OS, it will be /usr/local/go
  • You can check your GOROOT folder by running below command
    ➜ ~ go env GOROOT
    /usr/local/go

 

2.2 GOPATH

  • This is the folder that contains your Go Workspace (folder where you put all your source code)
  • Default GOPATH is the go folder inside your home folder. e.g $HOME/go on Mac OS.
  • You can check the GOPATH by running below command
    ➜ ~ go env GOPATH
    /Users/in-sunit.chatterjee/go
  • You can also set your GOPATH to any other folder also.

 

2.3 Workspace

  • Workspace is a folder that contains the Go Source Code.
  • This is what a typical workspace looks like
    Screen Shot 2018-12-09 at 10.15.09 PM

It contains two folders generally

  • bin
    • contains all the executables for the projects
  • src
    • 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 GOPATH folder. If your source code is in any other folder, then the package imports may not work.

Top ∆

 

3.0 Basic Go Commands

Before we go further, we assume you have some familarity with shell commands like ls, cat, etc, as we will be using them in our examples.

3.1 Building Go Source Code

  • Run command – go build
  • File Name is optional
  • Example – Building a single file.

    Screen Shot 2018-12-10 at 12.13.28 AM.png
    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.

    go build


3.2 Formatting a Go Source File

  • Can be done using command – go fmt
  • Formats the go source code in the file
  • Example – See contents of hello.go before and after formatting

    Screen Shot 2018-12-10 at 12.19.11 AM.png

3.3 Executing a Go Program

We can execute a Go Program by command – go run [filename]

Example

➜ 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 go install.

Example:

3.5 Cleaning build / test / object / temporary files, etc

go clean

Top ∆


In the next blog of the Learn Go tutorial series, we will look at Data types and Variables in Go