Wednesday 17 December 2014

G'day World in Go

I've got an odd situation at present... I have a suspected lump on the inside of my eyeball, underneath my retina. I say "suspected" because the symptoms I have are those that I've had before with my other eye, and "a lump" was what it turned out to be. The manifestation of this lump is that I have a "dull" patch in my vision, caused by the light hitting the wrong part of my retina where the lump is, and my brain not quite being able to adjust for it. I'm in the NHS waiting list to get it seen to, but that's not until Dec 31. Last time the treatment was some pills, so I hope it's the same again, rather than it being like something from Un Chien Andalou. Anyway... I'm telling you this because currently I am off work due to only being able to look at a screen for a few minutes at a time, before my head starts hurting. The dull patch is to the immediate right of my focus when reading, meaning the word following the one I am currently reading is "greyed-out". Frustrating as f***.

I'm finding this jolly tedious, have decided to try to write something today, to check how long it is before I need to give up. Hopefully it'll also give my brain some training to work around the dull patch. In my vision, I mean.

So - other than the fact I'm already at the 250-word mark with completely off-topic nonsense (what is this? CFHour? ;-) - here's a short article on my experiences of getting Go up and running on this box. it's very straight forward, and this article really only serves to demonstrate how straight fwd it is, so as to perhaps give people encouragement to try it out.

I'm running on Windows 7 on this machine.

First up I Google "golang" (expecting just googling "go" is not going to be the most fine-tuned of search strings). There it is, first match: "The Go Programming Language". One cool thing I note here is that there's a "Try Go" box right there, top left on the home page:

Railo should look to do something like this on their home page too. Perhaps they could liaise with Abram Adams of fame to get that integrated into their site?

Anyhow, the download link is right next to that, the Windows 64-bit MSI is one click from there. I'm pleased that languages are getting away from this "download the source, compile it, sacrifice warm-blooded mammals to the god obnoxia, [etc]" to get things running. It chugs away and installs itself, and adds a coupla bits to my path & environment:

Environment: GOROOT: C:\apps\go

Path: C:\apps\Go\bin

The site provides a "Hello World" example, so I saved and ran that:

// gdayWorld.go

package main

import "fmt"

func main() {
    fmt.Printf("G'day, world\n")

I dropped down to a command prompt in that directory, and attempted to guess how to run it:

C:\src\go>go run gdayWorld.go
G'day, world


Disclosure: I tried just go gdayWorld.go to start with, but that was wrong. Fortunately there's a go help option which set me straight.

So that's that working.

Next I checked to see if SublimeText has a plug-in for go, and indeed it does: GoSublime. This installs fine via package manager, but it then warns me I do not have "GOPATH" set in my environment. Indeed I do not. I've had to google what that was all about, but added it into my [Sublime Text user dir]\Packages\User\GoSublime.sublime-settings file, copying the setting name from [Sublime Text user dir]\Packages\GoSublime\GoSublime.sublime-settings:

    // you may set specific environment variables here
    // e.g "env": { "PATH": "$HOME/go/bin:$PATH" }
    // in values, $PATH and ${PATH} are replaced with
    // the corresponding environment(PATH) variable, if it exists.
    "env": {
        "GOPATH" : "C:/src/go"

Restarting Sublime, the warning is gone, so I am guessing that's what it wanted to know.

Indeed now I get some code completion assistance, and it tells me when I have a typo. Which I anticipate having many of, so this will be helpful.

It also gives me a little inline runner thingey (sorry for the technical jargon there):

Nice one!

OK, that's "G'day World" in Go dealt with, I think. And that's also about enough screen-looking for the time being: time to listen to a podcast through my eyelids. Pleasingly I now have no excuse not to grab Adam Presley's solution for my code quiz from ages ago, and start having a look at it. Stay tuned...