Thursday, July 9, 2015

Finally Starting To Pick Up And Go

My current employer offers Professional Development as one of the benefits it offers its employees. It's not quite the old Google 20% time, but it's pretty close. So I finally have a little time to spend on the Go language, which I've been wanting to pick up for a while now. I'll blog here from time to time related to what I find. The first thing I'll mention is that I've always worked at MS shops, and this job is no different. All of the Go tutorials and introductions worth a damn are on MacOS or Linux, so here are a few things to be aware of when first picking up the language and running the hello world program

Getting Go

Grab the MSI for the latest stable version here https://golang.org/dl/

The Official Getting Started Guide

https://golang.org/doc/install

Setting GOPATH

The guide uses the export command. This doesn't exist on windows. On windows, open up a command prompt and create a directory for your Go workspaces. I called mine c:\mine\golang\workspace. Then at the command prompt run

c:\mine\golang\workspace>set GOPATH=c:\mine\golang\workspace

Note that just setting this in the command prompt will mean this variable is only available in the current instance. If you were to open up another command prompt and use the echo command to view the variable (echo %GOPATH%) you would not see the value you just set. If you are going to make this your permanent workspace directory you should set the GOPATH in your environment variables under System Properties.

How To Write Go Code

https://golang.org/doc/code.html

Under your newly created workspace directory add your pkg, bin and hello directories. The Writing Code guide and the linked screencast talk about adding a path underneath linked to your github account. I didn't bother with that. For the hello world program I simply created a hello directory under src. So after writing the hello world program in src\hello\hello.go, I was able to run the following

c:\mine\golang\workspace>go install hello

This writes hello.exe to c:\mine\golang\workspace\bin\hello.exe, which you can run as instructed in the tutorial

After doing this I have the following structure in my folder.
C:.
├───bin
│       hello.exe

├───pkg
└───src
    └───hello
            hello.go

Hopefully this is enough, combined with the existing online material, to get you writing programs in Go on windows. These may seem like trivial little things, but the idea of workspaces and using directory structures like this will seem foreign to people using nothing but Visual Studio for their development work, which hides a lot of this underlying work from you.

No comments: