Go is fucking fun #golang
— Jason Connell (@jasontconnell) December 19, 2014
Favorited by Google's own @bradfitz. Watch the videos people! Particularly about Go's http2 server. Guess who wrote it? Brad Fitzpatrick.
Anyway, now tha tthat is over. Last year I was tasked with doing the group's Fantasy Golf tournament tallying for a weekend. It was prevoiusly done through Excel. I got it for one weekend and found out that the pgatour.com site offers all of the data for free. So I was determined to see if I could write an app that keeps track of the scores and the season scoreboard, the tournament scorecard, etc. I could, apparently. Read about the node.js implementation.
So that was last year. This year I'm playing with Golang, and I'm catching on with it quite quickly. It's fun. It makes programming fun and new. It's interesting. I'm learning new concepts, and new ways of doing things. So I started writing the site in Go last week at some time, and I finished it up today. The main cause of speed was that the database structure was already figured out, and all of the AngularJS code I wrote only need very minor tweaks, to be updated to some new ways I did things to streamline some processes, and how data is ushered between the client and server.
Here is example code to get the current tournament in node.js. The current tournament was either the last one, or if there's one starting in the next 4 days. Pretty simple.
this.getCurrentTournament = function(db, callback){
var upcoming = new Date().addDays(4);
var past = new Date();
dbhelp.find(db, "tournaments", { "$or":
[
{ "startDate": { "$lt": upcoming } },
{ "inProgress": true }
]
}, null, { startDate: -1 }, function(tournaments){
if (tournaments != null && tournaments.length > 0){ return callback(tournaments[0]); }
else return callback(null);
});
}
And here's Go code:
func (repo *FantasyGolfRepository) GetCurrentTournament() Tournament {
upcoming := time.Now().Add(time.Hour * 96) // 4 days
list := []Tournament{}
var tournament Tournament
query := bson.M {
"$or": []interface{}{
bson.M{ "startDate": bson.M{ "$lt": upcoming } },
bson.M{ "inProgress": true },
},
}
repo.OpenCollection(Tournaments).Find(query).Sort("-startDate").All(&list)
if len(list) > 0 {
tournament = list[0]
}
return tournament
}
The thing I like most about Go is that it feels safe. It's all typed, you're not accidentally stuffing a string inside a date field, or passing integer ID to the bson.ObjectId field. The main thing I like is that I feel safe putting this code out there in production, and when I go back to it in a year, it will make sense, it will work, it will compile. My database won't be in shambles by some mistyped keystroke.
I have written two web applications in Go, and they're pretty solid. And I've written a bunch of little utility programs. They vary. One program read xml and used the `xml:"element > subelement"` property attribute syntax. Some deal with JSON, others deal with making thousands of web calls.
Shit, I had a lot more written but I accidentally hit the wrong button in the browser and it's all gone now...
You can view the site at fantasygolf.jasontconnell.com