Declaring variable types

August 14, 2023

Today I’ll be love coding again. I hope you can join me! I’ll be working on the backlog for my open-source CouchDB SDK, First some light refactoring, then adding a long-missing feature. Join me to see how many mistakes a senior Go dev makes while coding.

We ended last week with a look at variable declarations. Today we continue that, with a look at how a variable’s type is determined. This can be a bit subtle.

Variable declarations

If a type is present, each variable is given that type. Otherwise, each variable is given the type of the corresponding initialization value in the assignment. If that value is an untyped constant, it is first implicitly converted to its default type; if it is an untyped boolean value, it is first implicitly converted to type bool. The predeclared value nil cannot be used to initialize a variable with no explicit type.

var d = math.Sin(0.5)  // d is float64
var i = 42             // i is int
var t, ok = x.(T)      // t is T, ok is bool
var n = nil            // illegal

Let’s look at some examples, from most to least specific, to try to make this as clear as possible.

// Variable type provided explicitly
var int x = 3 // x is of type `int`, and has a value of 3
var int y     // y is of type `int`, and gets the zero value for int, which is 0

// Variable type omitted
var z = int(7) // Type is provided in the corresponding initialization value (int)

const MaxItems = 100
var a = MaxItems // Variable assigned to untyped constant gets default type (int)

var b = nil // illegal, no default type can be determined

type Person struct {
  Name string

var *Person b = nil // Legal, the type is known, and can be nil

Quotes from The Go Programming Language Specification Version of August 2, 2023

Share this

Related Content

Where declarations are allowed

First, a big thanks to everyone who showed up for yesterday’s first Boldly Go Live stream. We had some audio problems, which I promise to fix before the next one. But otherwise it was a success, with some great discussion. You can catch the replay if you missed it. Here we are at the formal description of declaration syntax. Declarations and scope … Declaration = ConstDecl | TypeDecl | VarDecl .

No short declarations at package level

A couple last points about short variable declarations before we move on… Short variable declarations … Short variable declarations may appear only inside functions. In some contexts such as the initializers for “if”, “for”, or “switch” statements, they can be used to declare local temporary variables. So first: package foo id := 1 // Invalid. Short declarations only possible within fucntions var id = 1 // But this is valid. Don’t ask me why this rule exists.

Short variable declarations, cont'd

Today we’ll look at one of the most interesting, and I would say, confusing, aspects of short variable declarations. Short variable declarations … Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration.