Table of Contents:

Checking for Nil

Checking if a variable is nil in Golang sounds easy, but there is a common gotcha here.

package main

import (
    "fmt"
)

type Named interface {
    Name() string
}

type Thing struct {
    name string
}

func (t *Thing) Name() string {
    return t.name
}

func main() {
    var n Named
    var t *Thing
    n = t
    if n != nil {
        fmt.Println("n is not nil")
        fmt.Printf("the name of n is: %s", n.Name())
    }
}

When the code above runs, it prints:

n is not nil
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x49edfd]

goroutine 1 [running]:
main.(*Thing).Name(0x0)
	.../main.go:16 +0x1d
main.main()
	.../main.go:26 +0xbb

What is going on here? In golang, a reference to an interface is not nil if the implementation type is known. There is a workaround for this problem.

package main

import (
    "fmt"
    "reflect"
)

type Named interface {
    Name() string
}

type Thing struct {
    name string
}

func (t *Thing) Name() string {
    return t.name
}

func main() {
    var n Named
    var t *Thing
    n = t
    if n != nil {
        fmt.Println("n is not nil")
        if !reflect.ValueOf(n).IsNil() {
            fmt.Printf("the name of n is: %s", n.Name())
        } else {
            fmt.Println("but the value of n is nil")
        }
    }
}

When the above code runs, it prints:

n is not nil
but the value of n is nil