Sunday, January 8, 2017

Getting started with golang

You would think that as a polyglot, jumping into a new language would be pretty easy, and while Go’s syntax is pretty easy to get used to, this language actually had some hurdles to get used to in the development environment.

In this post I will talk about installing Go, setting up a project directory, and building a simple Hello World. While this doesn’t sound that complicated, you will be a bit surprised.
First and foremost, you will need to install the Go tools, this is pretty simple, head over to and grab the Windows amd64 installer. “Wait, I have an Intel isomething”, that’s fine, this is something that confuses a lot of people. Because AMD came up with the 64bit extension, they called it amd64, however Intel had a lot of issues figuring out what to call it, so most software just calls it amd64. If you happen to be one of those guys who have an x86 or ARM, there are downloads for those as well, just not at the top of the page, as most development environments are usually 64bit.

This is a pretty simple installation process, once complete, open up a command prompt and type “go env”, you should see an output like this:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++

These are all of the environmental variables associated to tell Go how to run/compile your application. There is one primary thing in here we care about right now, and that is the GOPATH.

What is the GOPATH, this is essentially what most programmers identify as there base project directory, but this is NOT your source code directory. Generally when developers check in code, this would have been the directory to check in, however in Go, this is actually considered bad practice, because then your project is not “go get”able. For this walkthrough, we are going to configure a static GOPATH so that we can have a central place to work on projects, as well as add the compiled versions to our path. In your file explorer, right click on “This PC” in the left navigation and select “Properties”. This will bring up the system configuration “Control Panel\System and Security\System”. From there, click on the “Advanced System Settings” again in the left side nav bar. Next click on the “Environmental Variables” button near the bottom of the dialog.

Ok, now in the top section of the Environmental Variables (user) click on “New” set the Variable Name to “GOPATH” and the Variable Value to a path on your machine, I generally have code on a secondary drive on my computer, and have a “projects” directory, so for the rest of this sample, lets assume that your GOPATH is set to “d:\projects\go”

When compiled, go apps by default will compile into the %GOPATH%\bin directory, I personally add this to my user path so that I can easily access these via command line without typing the path, this can be done by appending %GOPATH%\bin; to your Path environmental variable in the same interface. When finished, you must restart any command prompts you have open to reload the variables.

All of this, and we are finally ready to start writing some code. Below is a quick couple of command that are pretty self explanatory, but will create the structure you need.

mkdir .\src\hello
echo package main > .\src\hello\main.go
notepad .\src\hello\main.go

The last step will open the code for your application in notepad, which is fine for this example.
Here is a basic “Hello world” implementation:
package main

import "fmt"

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

This is pretty straight forward code here, this won’t cover how to code in go, more on how to get go running.

You can run your application by typing “go run .\src\hello\main.go”, and finally you can build your code with “go install .\src\hello\…” which will compile a windows executable for you into .\bin\hello.exe. Notice that the name of the executable is the name of your directory, this is important to note that go install will build all code into separate for each package main it finds with the name of the parent folder by default, if for instances you have multiple executables, you just need a separate directory for each one and define them as “package main” .

That’s really all you need to get started with a go environment to start playing with code.

Bonus Round: 
Compile a binary that can run on linux from your windows dev machine:

set GOOS=linux
go install .\src\hello\...
set GOOS=windows
dir .\bin\linux_amd64\

As you can see, this compiled an executable you can copy over to a linux machine and as a native application, all from your windows environment!

No comments: