Golang 101

Do you want to be a developer, and sustain a fun and meaningful IT Marketplace? Just click HERE.

Download Go

Installation

Download the latest version of Go at https://golang.org/dl. Choose the appropriate file depending on your operating system

  • For Windows 32-bit – download the file ending with windows-386.msi
  • For Windows 64-bit – download the file ending with windows-amd64.msi
Go Installation Folder

By default, Go for Windows will install it at C:\Go. The Go installer will automatically set the Path system environment variable

For other operating systems, please follow instructions at https://golang.org/doc/install

Setup Go workspace

From http://golang.org/doc/code.html

Go code must be kept inside a workspace. A workspace is a directory that is imposed by the Go language that contains at least 3 sub-folders namely:

  • src – contains Go source files organized into packages (one package per directory)
  • pkg – contains package objects
  • bin – contains executable commands

Under Windows, it is a convention to create the Go workspace under C:\mygo

Under C:\mygo, create three more sub-folders named bin, pkg and src.

  • bin
  • pkg
  • src

Your folder structure should be:

  • C:\mygo\bin
  • C:\mygo\pkg
  • C:\mygo\src
The GOPATH environment variable

Once you have setup the Go workspace (C:\mygo), create a new system environment variable named GOPATH and put C:\mygo as its value.

Under Windows 7, access Control Panel\System and Security\System\Advanced system settings\Environment variables\System variables, click New.

Reboot your computer to reflect the changes.

Download GitHub client

Installation

Go was designed to access open source third-party packages from repositories like GitHub so you need a GitHub client depending on your operating system.

For Windows, download at https://windows.github.com

For Mac, download at https://mac.github.com

For RPM-based Linux, sudo yum install git

For Debian-based Linux, sudo apt-get install git

Resource: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

For Windows, you need to ensure that Git has been added to your system variable named Path.

For example,

Reboot your computer to reflect the changes.

Download LiteIDE

Installation
  • Download the latest version of Golang light editor (LiteIDE) at https://sourceforge.net/projects/liteide/
  • Unzip the file at the folder of your choosing (e.g. C:\LiteIDE30). Run the LiteIDE executable (e.g. C:\LiteIDE30\liteide\bin)

liteide

Hello World

Your First Program

Remember that under Windows, the Go workspace by convention is located at C:\mygo (or at any folder of your choosing). The same goes with non-Windows operating systems.

The Go workspace (C:\mygo) consists of three folders named bin, pkg and src.

The src folder is the location of your Go programs such that there is one program per folder.

To illustrate, let us create a folder named hello under src.

It should look like C:\mygo\src\hello

Under hello folder, create a new file named main.go like C:\mygo\src\hello\main.go

Contents of main.go

litehello

 

//main.go
package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
}

Go is a compiled language.

package main

This indicates to Go compiler that this is an executable program.

import "fmt"

import is a reserved Go keyword which means we have to import or use a built-in Go package named “fmt” that prints output to the screen.

func main() {
}

This is the body of the program. Like the body of a letter, you write here the instructions that you want to be executed.

fmt.Println("Hello World")

This is the code that prints “Hello World” to the screen.

Compile the program

The program is our instructions to the computer. But the computer understands only zeros and ones (binary) so we need a process to translate the text-based program into a format that the computer will understand. Such process is called compilation or building the program.

Pressing the B blue button on the upper part of the LiteIDE editor will build the program.

litebuild

Alternatively, you may also build the program using the CLI (command line interface).

litebuild2

Then at the terminal, just type go build. It will produce the same result (hello.exe) assuming there are no compilation errors.

litebuild3

Running Your First Program

From LiteIDE, you will see that Go outputs hello.exe as a result of successful compilation or build. By convention, Go names the resulting program or executable with the same name as the folder where the program (main.go) is located.

To run, just type hello in the terminal.

literun

Go Get Packages

Go ships with a standard library. Outside of the standard library are packages that reside in external repositories like GitHub.

To illustrate, consider the following example:

package main

import (
    "fmt"

    "github.com/antonholmquist/jason"
)

func main() {

    exampleJSON := `{
    "name": "Walter White",
    "age": 51,
    "children": [
      "junior",
      "holly"
    ],
    "other": {
      "occupation": "chemist",
      "years": 23
    }
  }`

    v, _ := jason.NewObjectFromBytes([]byte(exampleJSON))

    name, _ := v.GetString("name")
    age, _ := v.GetNumber("age")
    occupation, _ := v.GetString("other", "occupation")
    years, _ := v.GetNumber("other", "years")

    fmt.Println("age:", age)
    fmt.Println("name:", name)
    fmt.Println("occupation:", occupation)
    fmt.Println("years:", years)

    children, _ := v.GetStringArray("children")
    for i, child := range children {
        fmt.Printf("child %d: %s", i, child)
    }

    others, _ := v.GetObject("other")

    for _, value := range others.Map() {

        s, sErr := value.String()
        n, nErr := value.Number()

        if sErr == nil {
            fmt.Println("string value: ", s)
        } else if nErr == nil {
            fmt.Println("number value: ", n)
        }
    }
}

In order to build this program, you need to pull that package from GitHub using a Go command:

go get github.com/antonholmquist/jason

Once it is done, you can see that package at C:\mygo\src\github.com\antonholmquist\jason assuming C:\mygo is your workspace.

Output
age: 51
name: Walter White
occupation: chemist
years: 23
child 0: juniorchild 1: hollystring value:  chemist
number value:  23
Advertisements

One Comment Add yours

Subjectivity aside, leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s