Site icon Shine Technologies

Faster, Higher, Stronger! Go Go Go!

gogo

It was a Friday and I was thinking about what to do on the weekend. My better half was going shopping with her friends, and I was choosing between bike riding on the Great Ocean Road or having a garage party with friends.

Then, just before I was going to shut my laptop, I took a quick look at Reddit and saw a post about a challenge that the guys from Go Challenge run each week.

So that’s what I decided to do that weekend…and the next 3.

What is Go?

Go is a programming language that was developed by Google and released in 2009. Whilst it’s pretty new, it’s also production ready. The most well-known example of its real-world usage is Docker.

Go is a compiled, statically-typed language with automatic memory management (using a garbage collector). My personal first feeling was “WOW, it’s C on steroids”. The building blocks are the same – structures and functions. The additional constructions are packages, interfaces and functions’ receivers. Also, a function is an object and you can utilize FP paradigms as well. Moreover, you can specify function signatures as arguments and return types.

The structure of packages is similar to Java, meaning that it’s a tree structure where each folder is a package. However, encapsulation can be done only on package level. There are only two types of visibility:

[code language=”java”]
//Package foo provides shows
//how encapsulation works.
package foo

//CounterHolder is exported and
//accessible outside foo package.
type CounterHolder struct {
counter int
}

var (
//internalCounter is private and
//reached only from foo package
internalCounter = new(CounterHolder)
)

//Increment is exported and
//visible outside the package
func Increment() int {
internalCounter.counter++
return internalCounter.counter
}
[/code]

Some other major highlights of the language:

What do I need to start?

The docs are the best place to start. Then just install Go or work your way through the online interactive introduction ‘A Tour of Go‘. I also recommend using the Golang Docker images. For me these were the easiest way to run the latest version on Linux.

Another doc that I highly recommend to keep open while developing is Effective Go. It helped me a lot when I was struggling with constructions that were new for me.

Finally, there are these communication channels:

What was my first Go Challenge?

My first Go Challenge task (although it was actually the fifth in the series) involved creating a tool that could find all public symbols that are not being used and converting them to be private (in Go, any definitions that start with a capital letter are public). Also, it had to prevent naming conflicts that might come up during the renaming.

I broke down the problem to the following  sub-tasks:

This particular Go Challenge was intended as a workout of the new go/types package that was released in version 1.5. Using this package you can check source code and extract information about types, definitions, usages. Whilst it mightn’t sound like I had to do much, the output API is very low level, meaning I had to create my own internal structures and wire up a lot of pieces.

The most difficult part was function arguments that had a type of some interface. I was stuck couple of times and even ended up raising an issue in Go’s Github project. It turned out not to be an issue after all, but I got a very quick and useful response (big personal thanks to Robert Griesemer). I took this as evidence that the community is functioning well and it’s easy to find answers to questions.

Sadly, I didn’t win that week’s Go Challenge, and if you compare my code with that of the winners (Robert Hovarth and Fatih Arslan) then it becomes obvious why. The main problem was that my code is basically like Java code written on Go.  In contrast, the winning solution is more compact, but not so much that it becomes excessively complex. Clearly I need more practice writing Go code!

My First Impressions

Here are my first-impressions about Go compared to what I normally using everyday (Java, JS, C):

Conclusion

Playing with Go has been a really nice experience and a great opportunity to discover a new language. It’s really cool when you can code something different and leave your comfort zone. I’m looking forward to participating in more Go Challenges.

Go is a great language with a lot of cool features. When you start coding, it charms you immediately and you want to use it more and more. Moreover it’s ready for production. I’m looking forward to use it in future projects!

Exit mobile version