Stuff I Wish I’d Known Sooner About Go — Part 1: Variables and Constants

Brandon Duffany
The Startup
Published in
3 min readFeb 21, 2021

--

Go is a nice, simple language that looks very familiar at first glance if you’ve used other programming languages before.

But it has a bit of a learning curve, since it does introduce a few novel concepts.

So I’m writing a few articles which would have saved me some time sifting through documentation and going through several rounds of trial-and-error on the Go playground.

In this first article, I’ll be talking about the basics, which are surprisingly subtle in some cases: variables. You’ll probably learn a thing or two even if you’ve already been using Go for a few weeks.

When to use var

Most of the Go code I have worked with avoids using var unless it is either required or if it makes the code clearer.

Here are the only cases where I think var makes sense to use:

When to use short-declare (colon equals)

The short-declare operator (:=) gives you a way to declare a variable and assign its value in a single statement, without needing to specify the type. Instead, the type is assigned based on the value you’re assigning.

You should use :=if:

  1. you’re declaring one or more new variables (and the equivalent var declaration would be more verbose), or
  2. you’re in a block initializer, such as if, for, select, or switch, and you want to declare a new variable — var isn’t allowed there.

Note: := is only possible when you’re introducing at least one new variable declaration into the current scope. If you’re trying to re-assign a variable in the current scope, use the plain old = operator.

Side note: when I first saw this short-declare syntax, my reaction was: “this feels a little confusing — why can’t we just use = everywhere when assigning, like Python?”

But the problem with using = everywhere is that it becomes hard to know which scope you’re targeting — do you want to declare a new local variable, or do you want to overwrite the one in the surrounding function? Or even a global? To solve this problem, Python uses nonlocal and global keywords when assigning to something that’s not in the local scope. Personally, these feel a bit clunky, and it’s easy to forget to specify them.

In Go, = always assigns to the value in the current scope, and you can use := to explicitly “shadow” declarations in outer scopes.

Here are some interesting examples where:= is used to shadow a variable declared in an outer scope (the second example requires some knowledge of functional programming and closures):

When to cast

Let’s look at the definition of time.Duration (from the “time” package):

What kinds of values can we assign to a variable of type time.Duration?

Check out this Go playground if you want to see some of the default types that go assigns to untyped literals.

That’s all I’ve got for this article! Please comment if you’ve got any tips you’d like to share which are useful for understanding how variables work in golang. Looking forward to sharing more Go tips!

--

--