Skip to content

Instantly share code, notes, and snippets.

@freeformz
Last active October 6, 2022 23:31
Show Gist options
  • Save freeformz/4746274 to your computer and use it in GitHub Desktop.
Save freeformz/4746274 to your computer and use it in GitHub Desktop.
Why I Like Go

A slightly updated version of this doc is here on my website.

Why I Like Go

I visited with PagerDuty yesterday for a little Friday beer and pizza. While there I got started talking about Go. I was asked by Alex, their CEO, why I liked it. Several other people have asked me the same question recently, so I figured it was worth posting.

Goroutines

The first 1/2 of Go's concurrency story. Lightweight, concurrent function execution. You can spawn tons of these if needed and the Go runtime multiplexes them onto the configured number of CPUs/Threads as needed. They start with a super small stack that can grow (and shrink) via dynamic allocation (and freeing). They are as simple as go f(x), where f() is a function.

Channels

The other 1/2 of Go's concurrency story. Channels are a typed conduit used to send and receive values. These are used to pass values to and from Goroutines for processing. They are, by default unbuffered, meaning synchronous and blocking. They can also be buffered, allowing values to queue up inside of them for processing. Multiple go routines can read/write to them at the same time w/o having to take locks. Go also has primitives for reading from multiple channels simultaneously via the select statement.

Compiled

Go compiles your program into a static binary. Yep, you read that correctly: a static binary. This makes deployment really simple, just copy over the binary. No bundler, no virtualenv, etc. All of that is handled at compile time. This simplifies deployment greatly.

Runtime

Go is a complied language, but still has a runtime. It handles all of the details of mallocing memory for you, allocating/deallocating space for variables, etc. Memory used by variables lasts as long as the variables are referenced, which is usually the scope of a function. Go has a garbage collector.

Pass By Value

Everything is passed by value, but there are pointers. It's just that the value of a pointer is a memory location, so it acts as pass by reference. This means that, by default, there is no shared state between functions. But, you can pass a pointer if you desire/need it for performance/memory utilization reasons. Go does the right thing by default, but doesn't shackle you. Oh, and there isn't any pointer math, so you won't screw yourself with it.

As pointed out on HN, you can do pointer math with the "unsafe" package and unsafe.Pointer.

Type System

Go has structs and interfaces. Go's structs can have methods associated with them. Structs can be anonymously included in other structs to make the inside struct's variables/methods available as part of the enclosting struct. Interfaces are sets of method signatures. Structs implement an interface by implementing the methods described by the interface definition. Functions can receive values by interface, like so. The compiler checks all of this at compile time.

Package System

Or lack there of. Since Go compiles everything statically, you don't have to worry about packages at runtime. But how do you include libraries into your code? Simply by importing them via url, like so: import "github.com/bmizerany/pq". Go's tooling knows how to look that up and clone the repo. Also works with Mercurial, Bazaar & Subversion.

Anonymous Functions & Closures

Go supports anonymous functions that can form closures. These functions can then be passed around and retain the environment under which they were created, like so. This can be super powerful when combined with channels and go rountines.

Built in profiling

It supports pprof as part of the standard library. And with a very small bit of work, you can access profiling info via a http interface.

Defer

Ever forget to close a file descriptor or free some memory? So have the designers of Go. The reason for this is that you usually have to perform those actions far away from where the resources were opened/allocated. Go solves this problem with the defer statement. Each defer statement pushes a function/expression onto a stack that get's executed in LIFO order after the enclosing function returns. This makes cleanup really easy, after opened a file, just defer file.Close().

Comprehensive Standard Library

Go's standard library is pretty comprehensive.

It's Fast

Go compiles down to native code and it does it fast. Usually in just a few seconds.

Comprehensive Tooling Out Of The Box

Go do a go --help. Some of my favorites: go fmt, go doc, go fix go get go tool pprof & go test.

Straight Forward Testing

Super straight foward, no magic.

Simple C Interface

By using build directives you can integrate with C libraries really easily.

Straight Forward Syntax / Standard Formatting

The syntax is pretty simple, C like w/o all the crazy of C. But what's really nice is go fmt, which re-writes your code into the Go standard format. No more arguments over coding style!

Issues

With all of that said, it's not perfect...

Runtime

Go's runtime is not super tuned yet. By comparison the JVM has had over 18 years of development history behind it. But, for a 1.0.X runtime & language, Go is pretty damn good.

Garbage Collector

Go programs can malloc a lot of ram at times, even when the program itself isn't using much. Most of this shows up as VIRT though, so most linux systems just won't care.

1 CPU by default

This is going to change over time, but the runtime will, by default, use only one CPU.

@notzippy
Copy link

I agree with +neagix , this is intentionally a KISS type language which does not complicate things with generics.

@caruccio
Copy link

caruccio commented Apr 4, 2013

Hey! What about coffee? Go does not make my every-morning cup of sugar-free coffee with vodka! This language really sucks!

@melignus
Copy link

melignus commented May 9, 2013

I'm a self taught web developer which means that I have a few gaps in my knowledge base. I'm coming from 5+ years of C/Python/Javascript and have decided to pick Go as my next language just to keep up on things.

So far it's awesome. I like the fact that everything I want a general purpose language to do Go does. I can easily see my 'everything-stack' being reduced from Python/Redis/Celery to just Go, which is awesome. Don't get me wrong though, Python/Redis/Celery is an awesome stack that I've had in production for years now, but the thought of getting all that functionality with just Go is very very enticing.

Not to mention something about writing Go just feels good. It's a very aesthetic language in my opinion.

@gliptak
Copy link

gliptak commented Oct 16, 2016

https://golang.org/doc/faq#Why_no_multi_CPU

In earlier releases of Go, the default value was 1, but as of Go 1.5 the default value is the number of cores available. Therefore programs compiled after 1.5 should demonstrate parallel execution of multiple goroutines.

@zaydek-owl
Copy link

Simple typo at Runtime: Go is a complied language

@emead-indeed
Copy link

A slightly updated version of this doc is here on my website.

Broken link. I guess you changed the path from post to posts. This should link to https://icanhazdowntime.org/posts/2013-02-09-why-i-like-go/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment