Getting started with go

Sat, Nov 12, 2016



"Getting started with go"

Overview

Golang (or just Go) is a great language for building simple and reliable applications. Over the past years there has been a visible shift in the tooling community as teams throughout the internet rewrite their apps from ruby or python to Go. With a large community backing it there are many libraries and utilities already available to cut your development time.

Let’s jump in and get started

Initial Setup

There are official setup instructions on the official site. I’m on a mac though and prefer the quick easy setup process.

If you’re using Homebrew on mac, just install the golang package as follows

$ brew update
$ brew install golang

Make sure everything installed correctly. Restart your terminal window and type the command go. You should see the following.

$ go

Go is a tool for managing Go source code.

Usage:

 go command [arguments]

The commands are:

 build...

Hello World

Great, now that its installed, lets write our obligatory Hello World example. Make a file called hello.go and add the following contents

hello.go

package main

import (
  "fmt"
)

func main() {

 fmt.Println("Hello World")

}

Previously we had to worry about paths and where the file was located. In recent updates however setting the $GOPATH is not necessary. While they’re not required here, we’ll review paths in a future article as they’re still useful when dealing with packages and external dependencies.

So now that we’ve got our super complex app written lets run it. Go is a compiled language but the tooling comes with a handy run command that will build and execute your app in the same step. Simply tell it which file and you’re good to go.

$ go run hello.go

Hello World

Intro to packages

Go supports grouping code into packages to help organize and scope your code as well as utilizing third party dependencies. Packages are defined by two key elements. First the code needs to indicate the name of the package it belongs to as the first line of the file. In our Hello World script above you see package main in the first line. The second key element is that the file needs to reside in a folder of the same name as the package.

MyApp
  |
  - main.go
  - person/
    |
    - joe.go # package person

In the example above, i’ve depicted an application folder called MyApp with a file called main.go and a subdirectory called person. In the subdirectory person there is a file called joe.go. This file, joe.go will note package person as the first line.

Here are a couple tips to keep in mind

The Main Package An exception to the folder requirement above applies you’re dealing with the main package. Those files are assumed to be in the root of your project tree and the folder name doesn’t matter.

Package Names not Paths Unlike with other languages the package name noted within the file is not the full path of the enclosing folder. Rather the package lists only the current folder. Import statements however will include the path. Heres an example

MyApp
  |
  - main.go
  - person/
    |
    - employee/
      |
      - joe.go

Lets look at person/employee/joe.go

package employee // NOT person/employee or person.employee

import (
  "fmt"
)

func SayName() {

 fmt.Println("I'm Joe")

}

Now lets use it from the main.go file

package main // NOT MyApp

import (
  "fmt"
  "person/employee"
)

func main() {

 fmt.Println(employee.SayName())

}

Import pulls in all the files of a package

In other languages you’ll often see import statements that pull in a specific Class or module for use from a given package. You might see import package.myclass; in java, or from urllib import request in python for example. In those examples the system is including only myclass or request objects. Typically those are single files in those languages.

Here though we don’t deal with objects. Go is not an object oriented language. When we say import person/employee in go, we’ll pull in everything in that directory marked with the employee package name.

Heres an example

MyApp
  |
  - main.go
  - person/
    |
    - employee/
      |
      - name.go
      - team.go

person/employee/name.go

package employee // NOT person/employee or person.employee

import (
  "fmt"
)

func SayName() {

 fmt.Println("I'm Joe")

}

person/employee/team.go

package employee // NOT person/employee or person.employee

import (
  "fmt"
)

func SayTeam() {

 fmt.Println("From Marketing")

}

main.go

package main

import (
  "fmt"
  "person/employee" // NOT person/employee/name
)

func main() {

 fmt.Println(employee.SayName())
 fmt.Println(employee.SayTeam())

}

Intro to scope

Great so now we’re starting to organize our code but how do we scope things to be accessible only from within the package, or publicly from outside the package. In Java its easy we litterally say Public or Private.

In go scope is managed by the (case upper or lower) of the method. Lower case function names are internally scoped to the package where Upper case functions are public.

Example time

name.go

package employee // NOT person/employee or person.employee

import (
  "fmt"
)

func SayName() { // Public Scope

 sayJoe()

}
func sayJoe() { // Package Scope

 fmt.Println("I'm Joe")

}

main.go

package main

import (
  "fmt"
  "person/employee"
)

func main() {

 fmt.Println(employee.SayName()) // All good

 fmt.Println(employee.sayJoe()) // ERROR not accessible

}

Wrap up

There is a ton more to explore but I hope this has provide you with a few nuggets to help you in your journey.

Feel free to reach out with any questions or challenges you may have

Cheers

#go,