golang: My introduction to the Go programming language

Gopher image courtesy of [Renée French](http://reneefrench.blogspot.com/)

I recently discovered Go, a new-ish programming language fostered at Google. I jumped right in and wrote a blog engine with it. My background is PHP. I’m used to the fast and loose school of programming. Getting something out there that works and fixing it later. Using Go was a change for me. It was my first time working with a strongly typed language, a compiled language and something closer to C without a lot of OOP stuff baked in.

The syntax of Go is very strict. One little character out of place and the compiler will let you know. At first I found myself stumbling through compiler warnings and errors. The syntax isn’t too different than PHP or other C style languages. There are differences in the core theory of some types. Like the difference between an array and a slice. Where an array is statically sized, a slice is a dynamically sized abstraction built on top of an underlying array.

Jump in

I threw myself into learning Go. I setup a DEV instance and just started coding. It wasn’t pretty. In fact it was ugly code. But I was able to write code that did what I wanted it to do. I stumbled back and forth learning as I meandered through syntax and code patterns. The online documentation is really good, and help me understand the language and gave me good examples which to mimic.

Along with the many features of the standard library and tools Go has a web server as part of the net/http library. I was able to use this server to serve Go generated content. It listened on a localhost port, and requests were proxied from the front end by Apache. This is a nice way to integrate a Go application server with Apache running static content for existing websites.

Generating content

Go has its own templating language. In text and HTML varieties. The templates are straightforward and easy to understand, however this was my greatest challenge. The Go templateing language contains very little in the way of logic statements or conditional testing. So no if( value == value). The idea is that you make those kinds of decisions in the functions in your application and only send a final data struct to the template to be displayed. They have a boolean “if” statement to check if something exist or doesn’t. But there is no built-in way to check a value or see if it is greater or smaller or make a decision on whether to display it.

For templates you can create filters, which are functions that a template can call that act on the current data structure. Instead of the template checking a userType number to see if it is greater than the access needed for an action you would create a filter that would check the value-and then return a data value to the template.

I created a filter to check for “<!–more–>” (more) in the blog body and to cut off output in the template. This is used on pages with multiple posts where we want to only show a summary and not the whole blog post.

The Go way of things

The philosophy of Go is small, simple and direct. It’s not quite the same as Python’s “only one way to do it” statement, but it’s along those lines. Go wants to be it small and not clutter developers mind with too many things. They don’t have a “do” or “while” loop. Instead they suggest the use of a “for” loop.

Go is opinionated. There is one way you should format your code, and that there is a code formatting tool that makes it easy to do so. It’s philosophy that we should write code correctly not just for today, but we should also be doing it correctly for tomorrow. With PHP there are a ton of ways to format your source and every developer has a different opinion on what that is. But it doesn’t matter with go. That’s not part of the conversation any more. Everyone formats code the same way and moves on.

Go works for me. I can edit, compile, and test just about as fast as I could with PHP, and I get the added benefits of static types, direct syntax and fast compiled code. It has some idioms and quirks that will take some time getting used to, but I was productive faster than I expected—without having any background in C or C++.

golang: My introduction to the Go programming language by  
  programming  golang 
Like what you read? Share it:
  Facebook   Twitter   Google+