Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

Here is a kind of penance by the folks who made C


This is a quick step-by-step introduction to the Go programming language (GoLang).

Although unveiled in 2009, companies who have adopted Go in their products include Atlassian, Docker, Facebook, DigitalOcean, eBay, Heroku.

“Gothic” is Go’s way of saying idiomatic Go code just like Python has Pythonic.

Hello World Code in Go Playground

  1. A Tour of Go: https://tour.golang.org/list

  2. In https://play.golang.org/p/0Hz57BQdTA

    // hello.go
    package main
    import( "fmt"; "time")
    var now = time.Now()
    func main() {
     fmt.Printf("hello, world at %v",now)

    Example response, the default time format is of the time of initial commit:

    2009-11-10 23:00:00 UTC
    1257894000 epoch time

Go is an opinionated language:

  • Compiler doesn’t issue warnings, just errors
  • Unused local variables are an error
  • fmt is the single way to format output

Like C:

  • Strongly typed (must specify data type in commands)
  • Used by developers of C (Kernigan)
  • Uses its own compiler to create itself

Unlike C:

  • Go has no pointer arithmetic. See https://tour.golang.org/moretypes/1

Like Java:

  • Collects garbage
  • Is memory safe by default
  • Networking is baked into the standard library and runtime

Like both:

  • needs to be built (not interpreted like Python)
  • double slashes for comments

UnLike both:

  • there are no parentheses surrounding the three components of the for statement
  • low GC pause (100 microseconds)
  • fast compile to native code
  • Memory is zeroed if not explicitly initialized

Install Compiler

  1. If there was a previous version installed:

    Uninstalling /usr/local/Cellar/go/1.8… (7,017 files, 281.6MB)

  2. Use third-party software installers so new versions can be managed automatically.

    If you use Homebrew on your Mac, install using:

    brew install golang

    Alternately, MacPorts:

    sudo port install go

    Ignore the web page which says download installer from:



  3. Consider a paid, commercially supported, quality-assured Go distribution for big data, database connectivity, code quality, microservices/cloud development:


    NOTE: This was in “beta” as of April, 2017.

  4. See where the go command references:

    go version

    shows (at time of writing):

    go version go1.8.1 darwin/amd64

    Source files folder

  5. In an internet browser (Chrome or Firefox):


    Its contents is described here below.


  6. Fork the sample repository to your own GitHub account.

  7. Switch to a Terminal shell window.

  8. Create a folder to hold various Git repositories (personal preference here, replacing wilsonmar with your account name):

    cd ~
    mkdir gits
    cd gits
    mkdir wilsonmar
    cd wilsonmar
  9. Edit (~/.bash_profile on Macs) to create $GOPATH (replacing wilsonmar with your account name):

    export GOPATH="$HOME/gits/wilsonmar/golang-samples"

    Alternately, append by using this command:

    echo “export PATH=$PATH:/usr/local/go/bin” » ~/.bash_profile

  10. Navigate to the default home folder.

    cd $GOHOME
  11. git clone https://github.com/wilsonmar/golang-samples

    PROTIP: Go source code files exist within a folder of the same name and have a suffix of .go.

  12. Verify by getting the environment settings:

    go env

    shows (at time of writing), on a Mac:

    GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/j_/gh27gpxj1t58hyryfg9drvdc0000gn/T/go-build434742939=/tmp/go-build -gno-record-gcc-switches -fno-common"
    CGO_CFLAGS="-g -O2"
    CGO_CXXFLAGS="-g -O2"
    CGO_FFLAGS="-g -O2"
    CGO_LDFLAGS="-g -O2"

    Auto format

  13. Format source code according to Go coding standards:

    go gofmt [flags] [path ...]

    See https://golang.org/cmd/gofmt/ about flags


  14. Install and configuring goimports to automatically run on save to format and auto adjust import statements as needed:

    go get code.google.com/p/go.tools/cmd/goimports

    BLAH: I’m getting these messages:

    package code.google.com/p/go.tools/cmd/goimports: unrecognized import path "code.google.com/p/go.tools/cmd/goimports" (parse https://code.google.com/p/go.tools/cmd/goimports?go-get=1: no go-import meta tags (meta tag github.com/golang/go did not match import path code.google.com/p/go.tools/cmd/goimports))
  15. Setup vim to auto run goimports on save using vim-go:

    let g:go_fmt_command = "goimports"

    BLAH: Response is “-bash: let: g:go_fmt_command: syntax error in expression (error token is “:go_fmt_command”)”

  16. Setup reload web server when file change is detected:




    Unlike C#, Go is not associated with an IDE such as Microsoft Visual Studio. However, shortcuts and reformat are provided for text editors:

  17. Install

    • TextMate 2 has a Go bundle at http://macromates.com/download

    • vim has a vim-go package at https://github.com/fatih/vim-go

Usage of Go

The 2016 survey found that 40% of respondants used the language for less that 1 year.

The greatest use is in runnable/interactive program (CLI).

When asked what they like most about Go, users most commonly mentioned Go’s simplicity, ease of use, concurrency features, and performance.

When asked what changes would most improve Go, users most commonly mentioned generics, package versioning, and dependency management. Other popular responses were GUIs, debugging, and error handling.

The Go compiler is programmed in Go itself rather than C.


Go isn’t an appropriate fit for iOS, JS.

Due to its small size, Go can be a good fit for use on a Raspberry Pi small computer to receive and provide small amounts of data.

Source Code

https://github.com/golang/go was previously in Mercurial.

To create a new bug (issue) on GitHub:

go bug

Social #golang

Go users call themselves Gophers.

Highest rated Go questions





Gopher Slack (invite.slack.golangbridge.org)


Hacker News






Rock Stars’ videos

The Go language was invented within Google
by Robert Griesemer and Unix luminaries Rob Pike and Ken Thompson.[1]


Rob Pike (Commander, Google)

Concurrency Is Not Parallelism 20 Oct 2013.

Todd McLeod




PDF: An Introduction to Programming in Go 2012

Introducing Go: Build Reliable, Scalable Programs
from O’Reilly Media January 2016 Pages: 124

Mike Van Sickle



Mark Summerfield [1]

Book: Programming in Go: Creating Applications

Dave Chaney




Francesc Campoy

Google Developer Advocate


https://youtu.be/ynoY2xz-F8s Understanding nil

Nic Raboy

https://www.youtube.com/watch?v=t96hBT53S4U Create A Simple RESTful API With Golang

Brian Hatfield


Tweets about GC pause times

Free Tutorials






My Code Samples

Files in my CodeSamples repo. combine several aspects of the language as useful programs.

  1. In a Terminal shell window.

    hello-time.go template

  2. cd to hello-time, which I use as the basis for creating new files.

    • print the elasped time of the program run.
    • define a date
    • calculate days from hours
    • format dates based on a template
    • see https://coderwall.com/p/l7e3fq/golang-time-function-wrapper
  3. The line at the top of the file enables it to be optionally run as a shell sript:

    chmod 555 hello-time.go
  4. Run it as a program:

    go run hello-time.go
  5. Build it

    go build

    A file with no file extension should appear in the folder on Linux. Extension .exe is added on Windows machines.

    When compiling packages, build ignores files that end in ‘_test.go’.

  6. Run the executable on Linux:


    Alternately, on Windows, run the executable:


    Random number generator

  7. cd to rand

    • Import flag to obtain arguments from command line
    • Reflection of elasped time is not appropriate here because it is intended to run as a utility.
    • Defined in a main plus separate utility functions.
    • Loop the number of repeats specified.
    • Slice of an integer array containing prime numbers. See https://tour.golang.org/moretypes/8
    • Pointer

    Loop until manually stopped

    Within a main infinite loop:

    func main() {
      var input string
      for {
  8. cd to switch

    • Print command to construct a line printed over several command lines
    • Recognize operating system being used by using “runtime.GOOS”.
  9. cd to strings

    • One Println with several commands.
  10. cd to latlong

    It manages latitudes and longitudes in a map data type containing a key-value structure.

    • Uses float64 floating point number for latitude/longitude
    • Uses function to do repeated lookups
  11. cd to utf8

    It provides examples of counting and comparing Asian characters encoded in UTF-8.

    • Provides an illustration of fmt.Print() vs. fmt.Println() with escapted double-quotes.
    • Provides an example of using range.
  12. cd to env-vars (in Git branch env-vars)

Data Types

boolean constants are lower case: true, false

Numbers are not permitted at the start of an identifier (variable).

Go detects unused imports as an error.

Go has an array type, but most interactions are with slices which are built off arrays. I don’t worry too much about the name and just use them The specification for slices are “[]T” where T is the type. So “[]string” is a set of strings, “[]int” is a set of integers, and so on.


var n int = 4
var i = 0
for i = 1; i < n; i++ {

int is actually an alias! Like C, Go has signed and unsigned integers:

  • int holds a maximum value of 32,000.
  • int8,
  • int16,
  • int32,
  • int64

UTF-8 Data Types

Alias rune are single-character constants: ‘a’, す’, ‘シ’, ‘1’, …

When working with unicode you should be converting your strings to []rune. That’s why the utf8 package is so sparse, most things are covered by []rune conversion and the unicode package. The utf8 package is like a very thin abstraction layer for using strings as []rune.

All source code in Go is UTF-8, so you can use an emoji, Japanese Kanji, or other language in variable names:

    var π = 3.14159
    var radius = 6371.0 // radius of the Earth in km
    var circumference = 2 * π * radius
    println("Circumference of the earth is", circumference, "km")

Run result in scientific notation:

   Circumference of the earth is +4.003014e+004 km

See https://coderwall.com/p/k7zvyg/dealing-with-unicode-in-go

Developer Tools

  1. Display documentation for the Println function within fmt:

    godoc fmt Println

    Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

goimports, golint: code quality

Delve: debugger

glog, spew: logging


The most popular Go packages on all of GitHub:

  1. https://github.com/stretchr/testify/assert

  2. https://github.com/gorilla/mux A powerful URL router and dispatcher for golang.

  3. https://github.com/Sirupsen/logrus by Simon Eskildsen @shopify
    Structured, pluggable logging for Go.

  4. https://github.com/gorilla/context
    A golang registry for global request variables.

  5. https://github.com/golang/protobuf/proto ???

  6. https://github.com/gorilla/mux
    A powerful URL router and dispatcher for golang. http://www.gorillatoolkit.org/pkg/mux

  7. https://github.com/codegangsta/cli
    A simple, fast, and fun package for building command line apps in Go.

  8. https://github.com/mattn/go-sqlite3
    sqlite3 driver for go that using database/sql

  9. https://github.com/go-sql-driver/mysql
    Go MySQL Driver is a MySQL driver for Go’s (golang) database/sql package

  10. https://github.com/BurntSushi/toml
    TOML (Tom’s Obvious, Minimal Language) parser for Golang with reflection. https://github.com/toml-lang/toml

  11. https://github.com/garyburd/redigo
    Go client for Redis

  12. https://github.com/golang/glog
    Leveled execution logs for Go

  13. https://github.com/onsi/ginkgo by Onsi Fakhouri @ Pivital
    BDD Testing Framework for Go http://onsi.github.io/ginkgo/

  14. https://github.com/onsi/gomega
    Ginkgo’s Preferred Matcher Library

  15. https://github.com/juju/errors at Ubuntu Canonical
    Common juju errors and functions to annotate errors. Based on juju/errgo. Model and deploy applications to any cloud

  16. https://github.com/juju/testing/checkers
    This package provides additional base test suites to be used with gocheck. Testing gocheck suites and checkers used across juju projects.

Testing/Error Handling

assert: extended core testing capabilities to write tests more easily

errors: improved error handling


if … { … } else { … }, switch { … }

Heavily nested if else if blocks are discouraged.

for (init statement); condition; (post statement) { … }

As Go is a strongly typed language, for two variables to be equal, both their type and their value must be equal.

while condition { … } and

do { … } while condition

do { … } until condition

// TODO: Control flow: break, fallthrough, continue, goto, return.

  1. cd to sync which uses Concurrency features.

    One of Go’s biggest strengths is that the concurrency features it provides are much easier to understand and use properly than threads and locks.

    Go has “goroutines” that are lightweight concurrency model based on coroutines. Goroutines do not use convoluted callback coding styles.

    The design of the Go language makes it work on multiple CPU cores. Go’s language runtime makes sure that there are enough threads to keep cores active.

    Go’s defer command.

    A defer statement defers the execution of a function until the surrounding function returns.

    The deferred call’s arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

    Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.

      for i := 0; i < 10; i++ {
     defer fmt.Println(i)

    See https://blog.golang.org/defer-panic-and-recover

Web server


A static web server to serve Jekyll generated static files:

// From https://coderwall.com/p/up2jbg/golang-static-files-server
package main
import "net/http"
func main() {
    http.Handle("/", http.FileServer(http.Dir("_site")))
    err := http.ListenAndServe(":8000", nil)
    if err != nil {

Protocol Buffers (Protobuf)



generates Go source files

go test -coverprofile

Output a cover profile as you are testing a package, then use go tool to visualize them on a browser.

go test -coverprofile=c.out && go tool cover -html=c.out


go test -bench=.

https://newrelic.com/golang monitor