Trying Out Go for the First Time

As a front end developer, I’ve heard of the Go programming language before, but haven’t really had a reason to look at the project in detail. That is until I realized that Go has package management and you can use Go for tooling, much like you can with Node and Ruby.

If you haven’t had much experience in the world of Node or Ruby tooling, the term refers to using the Node and Ruby platforms to install programs that help accomplish specific tasks while increasing productivity.

Go packages work a little differently than what I’m used to with NPM and Ruby Gems, so I figured I’d dive in head first and see what it takes to get started using Go.

A Go-verview

Before we get started, let’s take a quick look at what Go is all about and how it’s used to do cool internet things.

Go is a programming language that was initially developed at google by Robert Griesemer, Rob Pike, and Ken Thompson. It features a C-like syntax, adding in common patterns and types associated with more dynamic languages. Go compiles down to machine code, which means that it is executed directly by a computers CPU. Go is a system programming language, which means that it is designed to handle backend and system level tasks, such as creating servers, operating systems, and desktop applications. It’s worth noting that you can still build web applications with Go, much like you can with Node or Ruby. If you’re interested in learning more about Go in general, take a look at their language specification and FAQ page.

Go packages, like Node packages or Ruby Gems, are a little different than the traditional package installation model. When installing a Go package, you install it directly from an svn or git repository, using the syntax Go get [package url].

Go packages don’t have an explicit concept of versioning, which may seem a little strange to those who are more familiar with Node or Ruby tooling. Another difference, particularly different from node packages, is the minimal number of dependencies that any single Go package should have, a principle which is explained in further detail here.

Some Quick Advice

So now that we’ve gone over the Go programming language, let’s go ahead and try to install it! An important detail here, before now, I’ve never installed or used Go, so this is a look into installation from a complete beginners perspective. If you notice any errors I’ve made during installation, please feel free to leave a comment.

Heads Up! These steps follow the instructions for Windows machines. This installation process is likely a little more involved than for *nix and Mac users. Look out for windows specific terminology and headaches!

Installation

My first step was to navigate to https://golang.org and look for an installation prompt.

This brought me to https://golang.org/dl/, where I downloaded the Windows 64 bit installer. I ran the file and the installation process began.

Add Go to Your Path

This process didn’t take too long, but after everything was installed, nothing happened — which, honestly, was a bit unexpected. I then had to hunt for any sort of hint that would explain how to open a command line interface or anything else that would allow me to run Go, which I expected from my experience with Node and Ruby.

I decided to run to Google for help, and after some searching, found that Go should be able to run from Windows command prompt. This also meant that I needed to set a Go path environment varible, which would allow me to run Go from outside of the directory that I installed it to.

After setting the path environment variable, I restarted my computer and ran the command:

Go

I was very happy to see that my computer finally knew what I was talking about! Go has been successfully installed on my machine.

Running Our First Script

Of course there was one last thing I needed to do in order to ensure that Go was installed correctly, and that was to run a very simple Go program. I found a template that I could use on the Go website, created a file called scotch.go, and executed the program from the command line. Again everything worked as expected.


package main

import 'fmt'

func main() {
    fmt.Printf('Scotch.io is super cooln')
}

Then, from the command line, I executed the script by typing:


go run scotch.go

It simply executed the main() function and printed its contained statement. Here’s a snapshot of what it looked like:

Let’s Use “go get” to install and use our first Go package

So now that Go is installed and working properly, let’s try to install a Go package!

I was able to find an interesting package on gopm.io, a site which works sort of like npmjs.org. The Package I found was called pup, which is a command line tool for processing HTML. I attempted to install the package with:


go get https://github.com/EricChiang/pup

But, I got an error which informed me that I needed to set yet another path environment variable.

Setting Your $GOPATH

A gopath is a place where all of your Go packages should live. This needs to be set as a path environment variable so that you can install Go packages without having to change your working directory every time you run the go get command. So I set the gopath path environment variable, ran the go get command again, and was surprised to see yet another error. This one was an http proxy error, and while I’m still unsure of what exactly the problem was, I found a StackOverflow.com thread that explained how to fix this particular issue, by running the following command in Git Bash:


git config --global --unset http.proxy

So I ran the git command, switched back over to Go, ran the go get command again, and to my dismay yet again, was greeted with yet another error.

`go: missing Mercurial command. See http://golang.org/s/gogetcmd`.

I’d never seen anything like this before, so I decided to check Google to see if anyone else had seen this error. It turns out that if you’d like to use the go get command, you need to install Mercurial and Tortoise (which is installed with Mercurial) needs to be set as a path environment variable. I was able to set this path environment variable quickly by running the following command from command prompt:


set PATH=%PATH;C:Program FilesTortoiseHg

At this point in time it was getting very late, and I was very happy to find that running:


go get github.com/ericchiang/pup

Finally worked as expected with no additional errors!

This specific package “pup” uses cURL, which is provided by bash. I switched over to git bash for this last part. I then changed my working directory to the folder where the correct package was located, and ran:


curl -s https://news.ycombinator.com/ | pup 'td.title a[href^=http] attr{href}'

Again, no errors this time!

Conclusion

Installing Go and using a simple package took me around 4 hours. I started at 930pm and didn’t finish until 130am. This is partly due to my inexperience with Go and how it works, and partly due to a lack of documentation and beginner tutorials. Whatever the reason may be, it seems that the Go community could do a little better at helping beginners and general enthusiasts.

Aside from the installation difficulties, Go seems like a great language with a growing community and a large number of packages. If you’re looking to add a new skill to your arsenal, or just want to see what a powerful new language has to offer, I fully recommend giving Go a spin. Feel free to leave a comment with all of your Go comments, questions, and resources!

Additional Resources

Tim

Tim is a Front End Engineer who spends his free time writing, enjoying a glass of lagavulin neat, and writing short third person bios.