Go is a statically typed programming language. If you only worked with JavaScript, Ruby, Python, or other dynamically typed languages before, there are a couple of new things to learn. But even for C++ or Java programmers, there are a few aspects where Go is different.

Variable Declaration

Let’s start by declaring some variables.

There are a couple of ways of declaring a variable. First, we can declare a variable with the var keyword, followed by the variable name, followed by the type name.

/* At package level, outside any function */
var a int

This creates a variable named “a” of type “int” with an initial value of zero. The last part is an important detail. Go does not have the concept of undefined variables. Each data type has a well-defined zero value that is used when the variable is not explicitly initialised to something else.

So if we print out the value of a,


we get no error even though we have not assigned anything to the new variable yet.

It is also possible to assign a non-zero value at the point of declaration.

var b int = 10

If we omit the type, the Go compiler attempts to infer the type from the initial value.

var c = 10

In this example, c becomes an integer variable.

There can be more than one variable in a var declaration.

var d, e, f bool

We can also use the syntax that we have already seen with the import statement.

var (
    g int
    h string
    i int = 1234
    j, k, l bool

This leads to less typing and clearer code.

We can use all of the above also inside a function.

func main() {
    var (
        g       int
        h       string
        i       int = 1234
        j, k, l bool

Here we can also use an alternate style called “short variable declaration”. This actually is a statement, rather than a declaration, which is why we cannot use it outside a function. The short variable declaration is of the form

m := 1

and is declaration and assignment in one step. Multiple declarations are possible as well.

n, o := 2, 3


To assign a value to an existing variable, use a simple equal sign.

a = 11

Go also supports multiple assignments in one statement.

e, f = f, e

Here we effectively have swapped the values of the two variables e and f.

You can even assign to new and existing variables at the same time, using the short variable declaration operator.

a := 11
a, p := 100, 200

Static Typing

If you only used dynamically typed programming languages before, be aware that a variable cannot change its type at runtime. For example, trying to assign a string to a variable that has been declared as an int produces an error at compile time.

a = "wrong"

This may seem like a big inconvenience at first (if you are used to languages like JavaScript or Python), but keep in mind that this strict type checking may save you from having to painfully track down some erratic behaviour at runtime. And the compiler can better optimize for speed.

The Go compiler even goes one step further and also complains if a variable is declared but not used. That is, this code will not compile.

func main() {
    a := 1

Trying to run this code triggers the message, “a declared and not used”.

The Blank Identifier

In some situations, however, you need to declare a variable that you do not use later. For example, if a function returns two values but you only need one of them,

a, b := f()

then you can use the so-called “blank identifier”. This is simply an underscore that you use instead of a real variable name.

a, _ := f()

To Summarize

  • Declare variables using the var keyword.
  • The variable name comes first, then the type name.
  • Undefined variables do not exist. Each new variable gets initialized to its zero value.
  • You can declare a variable and assign a value in one step.
  • You can declare a variable without a type if you assign a value in the same step. The compiler will then deduce the type from the assigned value.
  • Inside functions, the short variable declaration operator declares a variable, assigns a value, and deduces the type of the variable in one single step.
  • Declaring and not using a variable is an error.
  • Use a blank identifier if you must declare a variable that is not used later.

There are a couple of advanced data types as well, and we will discuss each of them in an extra lecture.


Language Reference: Variable declarations

Language Reference: Short variable declarations

Effective Go: The blank identifier