Michael Crosby


Go Lang

If you don't know about Go it's a programming language for now. Most of the languages that we use everyday are old, 10+ years old. After so many years languages need to start to think about news ways to solve the same problems. Go is a new language that takes a different stance on how you write and deploy code. Go is a dynamically typed but statically compiled language. I have "played" with Go pre 1.0 a few times and at the 1.0 release but it never really stuck. I felt like I was writing to much code for what I wanted to accomplish. However, this last time working with Go 1.1beta I have started to do a few projects and packages with Go and I am really impressed with the language.

Abstractions

I write a lot of Python code and I still enjoy writing Python more than any other language so I am used to a fairly high level of abstraction from the language. Even working in C# with tools like LINQ you get trained to expect abstractions for working with collections. Also when used correctly, I really like having generics available in my language.

You miss these things with Go. Go is at a lower level of abstraction in many areas. Looping and working with arrays and slices ( variable sized collections ) is often back to the for i loop. Going from LINQ to anything less hurts. I find my self working with []byte more in Go than I have in any other language. The Go stdlib does provide you with some abstractions in the io packages but you still are working with byte arrays often.

Lets look at reading a file line by line in both Python and Go.

with open('names.txt') as f:
    for line in f.readlines():
        print line

Python is pretty straight forward. Very simple to remember and can be done with no imports from the stdlib. Now lets look at the Go example.

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    file, err := os.Open("names.txt")
    if err != nil {
        panic(err)
    }

    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }
}

So the go example is a little more than just 3 lines of code. We have the package declaration, 3 imports from the stdlib, and the go error handling code in the main func. Even though Go has more code it is still simple and elegant. You do have to learn the stdlib to do simple tasks but once you do, you have a very powerful set of tools. The go example is every more amazing once you learn about implicit interface implementation.

No Classes

I like OOP and modeling classes and their interaction with each other to create a fluent and straight forward API. However, I also feel like OOP is taken too far. People will insist that you have to design a polymorphic interface, provide factories, and strategies when all you really need to a simple function. I believe in design patterns for solving problems in software but I also believe in simplicity, clarity, and elegance in the code that I write. For me, simple wins over "over engineering." Knowing when to design and when to bang out a function comes with experience.

With Go you have types and interfaces but no classes and inheritance. At the beginning this was hard for me. However, after working with packages, types, and interfaces in Go I do not miss classes. I feel like my code is simple and straight forward with only types and packages. It is OOP at it's purest, data with actions. You can still attach methods to your types and you have public and internal access via upper and lower case names. If a type and/or function begins with an upper case it is exported to be used outside of that package, if it is lower case it is internal to that package.

Interfaces in Go are also very modern. You can have a type and implement an interface without explicitly stating that you implement that interface. If we have an interface that has a

Write([]byte) int

then in Go, any type that has a method Write that takes a byte array and returns an int implements that interface, which is exactly what you want. With interfaces you are basically saying that you don't care about the type or other methods, all I care about is that whatever I have passed to me has a Write method that takes bytes and returns an int.

Why Go

So why am I writing a post on a language with no classes and missing certain abstractions that we are used to? Because their some home run features that make Go an amazing language to work with.

Concurrency

There are many articles about concurrency in Go with gorutines and channels so I am not going to say much here except that they are awesome.

Compiled binary

Deploying a python application takes automation. You need to to create a virtual environment, have a requirements file with all of your dependencies, and then finally copy your application code up to the server. There are many tools for deploying python code and it really is not that bad but it is also not simple and there are many places that things can go wrong.

With Go you have a compiled binary with all the dependencies compiled into the binary. You do not even have to have the Go tool chain installed on the computer that you are deploying your go application to run the code. You just drop the executable on the machine and your good to go, nothing else to do. This is amazing.

Also being compiled into native machine code makes go very fast.

Testing and Packages

Testing is simple and easy. Automating testing is simple and easy. Have you tried to automate C# unit tests? Oh, you don't own VS Pro with unit testing?

Installing dependencies is very easy with "go get". You install dependencies exactly the same way as you have your own packages.

Go Doc

All of Go's documentation is auto generated by the commends in the code. This includes packages in the stdlib and your own packages in your GOPATH. I have been doing a lot of work in an area with no Internet access to I really appreciate being able to run the package documentation and other general Go documentation on my local machine.

godoc -http=:8080

This provides me with all the documentation and examples available if I was connected online to the golang.org website.

Parting words

Hopefully this has peaked your interest in Go and maybe you can give it a try. I am going to start using Go as my main language for backend services and processes.

comments powered by Disqus