Go (or golang) is a relatively new language that I personally discovered while building REST API servers for my internship over at OSNEXUS, and I quickly fell in love with the language’s speed and syntactic sugar. A powerful tool, especially when it comes to concurrency and web servers, Go makes a strong case to be used in your next project.

What Is Go?

  • Open source, compiled general purpose programming language.
  • Initially developed, partially maintained, and used internally by Google.
  • Announced in 2009 as an updated alternative to other languages like Java and C++.

Why Should I Use It?

  • It’s powerful, massively faster than Python and Javascript and easier to use than C and Java.
  • It’s general purpose, and can be used for anything you need to design, but really shines in applications that deal with lots of concurrency.
  • It’s becoming quite popular, among open source developers and large enterprises: http://www.benfrederickson.com/ranking-programming-languages-by-github-users/ (Kubernetes, Docker)
  • It scales well, but can stay lightweight for smaller projects.
  • It’s fun.

package main

import "fmt"

func main() {
fmt.Println("Hello World")
}

I often describe Go as if Python and C had gotten together and worked some stuff out. There’s elements of Python’s fun and lighthearted approach that are mixed with the structure and speed of C. This combination of speed, ease of use, and focus on scalability and concurrency has led it to be used in many of Google’s internal projects (Kubernetes), as well as the basis for many other large systems (Docker, Bitbucket, Lyft). Below is a little more fleshed out example of why Go might be worth picking up for its greatest use case, server development.


package main

import (
"log"
"net/http"
)

func main() {
router := router.NewRouter()

log.Fatal(http.ListenAndServe(":8080", router))
}

// Route holds an individual route within a routes list
type Route struct {
Name string
Pattern string
Method string
Handler httprouter.Handle
}

// Routes contains routes of a certain method.
type Routes []Route

var routes = Routes{
Route{
"Index",
"/",
"GET",
Index,
},
}

// NewRouter creates the main, new router.
func NewRouter() *httprouter.Router {
router := httprouter.New()
for _, route := range routes {
router.Handle(route.Method, route.Pattern, route.Handler)
}
return router
}

func Index() {
fmt.Println(“Hello World”);
}

Like C, Go runs the main method on execution. In this case, we create a simple HTTP router and then instruct it to handle our defined routes. When the route’s pattern is matched on the specified port, the corresponding handler method is fired. In this case, we only have one route, Index, which just prints Hello World. This is a common Go setup, and is easily scalable to larger systems. While Go might not be as mature as some other languages, it makes a strong case to be used in all your server side applications, as its speed and powerful concurrency tools make it far superior than other popular choices in those areas like Python, Node, and PHP.

Pros

Cons

  • While on the rise, Go’s still a niche language with not as many libraries or experienced users as something like Python or javascript.
  • This is a point of contention as to it being a con or not, but Go’s error handling is certainly a tad odd.
  • Package management is a bit odd and rudimentary at the moment, but improving.

If you have further interest in Go, I’ll be writing a few introductory tutorials and explanations in the coming months. You can also check out the official golang website along with the very informative Tour of Go to get some hands on experience.

Leave a comment

Your email address will not be published. Required fields are marked *

X