HostQ and a review of my first month in Go
Thirteen years ago, a group of friends and I started eating out every Friday night and going back to someone’s house for drinks and board games. After college we started getting married and rotating through 3 houses to serve a 3 course meal on fancy new wedding china. Then came children and suddenly we became a very LOUD potluck dinner on Saturday nights. More friends wanted their families to be included and now we have 12 adults, 10 children (plus one more on the way)! We have 5 houses we rotate through using a simple queue for who is hosting. Sometimes family events interfere with a couples ability to host or a holiday causes us to skip a week. By the time we added our fifth family to the rotation we couldn’t keep track of whose turn it was.
I had been told by not one but three friends recently how awesome Golang is and when I found myself repeatedly saying, “I’m not just a Java developer”, I decided their challenge was accepted. The first iteration is done and in production. I’ve asked those said friends for a code review and have heard back from two. I’ll discuss my initial thoughts on the good, bad, and ugly of Go and discuss a little about this app and my hopes for its future going forward.
#Good
Terse
I spend the last 6+ years in various Java environments. I am happy to have shorthand notation to instantiate variables, create structs and access their data.
Small vs Giant
Java takes the approach of the bigger the better; resources are cheap, use more of them. Go is very much the opposite. After fussing about having to write more code, for example, writing a contains method for my string slice, I’ve enjoyed thinking on the smaller is better scale. I had to say, though, that I am terrible at it so far!
Default Value
At the time of writing this article, a GitHub search shows there are 2,233,731 references to NullPointerException in code. What would happen if instead every object returned a default value for the field? Strings give an empty string, ints gave 0, and booleans defaulted to false? How would your coding change? Go does this very thing.
Dead Code
A variable that is instantiated and never used is called dead code. By default this gives a compiler error in Go. At first this drove me crazy because I wasn’t done thinking and getting all the code down, but it didn’t take very long before it showed me a couple places where it was a bug. One example was creating an issue object to create GitHub issues and I had formatted the error string, but never set it on the issue field. The compiler jumps up and down yelling, “Hey you! You forgot to do something!” and it is right I did forget to make Issue.Body contain the error string:
type Issue struct {
Title string `json:"title"`
Body string `json:"body"`
Labels string `json:"labels"`
}
func CreateGithubIssueAndPanic(err error) {
errString := fmt.Sprintf("%v", err)
var issue Issue
issue.New()
issue.Title = "Auto Created issue"
…
}
UTF-8
Have you ever been on a project already in production and all of a sudden you have to hurry up and implement a foreign customer? You start pulling out all the displayed text into separate files, get translations and start testing only to see, “???? ????? ??? ????”. Golang assumes you are working in UTF-8 as the default, one less thing for us to remember.
#Bad
This is a list of my percieved bad list. It may change as I get more familiar with Go!
Lack of Generics
I’ve done a lot of development in both Java and Python. Before I started getting into Golang I was imagining it to be more like Python than it actually is. Java has generics (https://en.wikipedia.org/wiki/Generics_in_Java) and Python uses duck typing (https://en.wikipedia.org/wiki/Duck_typing) in collections. I needed a simple contains method for a Go slice, but because of it’s lack of generics Go does not offer one.
Python’s list[-1]
One of my favorite features of Python was to get the last element by using [-1]. When I heard Go was a combination of C, Java, and Python I expected it to be there. Instead you have to get the length of the slice and subtract one. It is more visual clutter, but not a deal breaker.
Inheritance
Inheritance in Go is quirky. Everything is a has-a relationship. This is different from Java which supports single inheritance and Python that supports multiple inheritance. For my small app I have a nested struct and have not needed anything more. I’d love to look at large projects and see what they are like and if a better inheritance option would be nicer.
App engine specific: Nested struct with a slice
I tried to use a slice that contained emails, something I knew would stay small, on a nested struct. App Engine complained at this. I understand this can grow pretty fast and cause issues, but I felt that emails would not. A workound for this issue is to marshal to JSON for storage and unmarshal for use.
#Ugly
Error Checking
Is there an error now? How about now? Now? Error yet?? Is it an error that I haven’t said anything else?
#HostQ I’ve only had the opportunity to work on this application while my baby is sleeping and my preschooler is in quiet time. This is about 1 hour a day. Let me just say, learning a new language on a new platform with a new datastore, to create something you have promised friends (people who you care what they think of your work) is ROUGH. At first I thought I would never be able to read the different style of method signature and frequent compiler errors for dead code or unused imports, but giving it a little time I’ve enjoyed it. I have had some good feedback from my Gopher friends that you can check out here: https://github.com/AnnAddicks/hostq#code-review-suggestions-my-thoughts-in-parens There is so much enthusiasm from my dining in group that a subgroup wanted to have a beer rotation group added for their D&D nights. I’ve received a lot of excitement from them despite the fact that this is a super simple application. They have give some excellent feedback that you can checkout here: https://github.com/AnnAddicks/hostq#requests-from-dining-in-group.
#Is it worth checking out the language? Absolutely! Even if you plan to stay in statically typed languages learning new languages can benefit your programming by giving a new perspective on how to solve a problem. I have always wanted to learn Scala, but went with Go because I know people who I could ask questions and get some good feedback. Once I got over some pretty common complaints it became a fun language to accomplish my goals for our dinner hosting application.