Go

What does this print? (nil interface vs interface holding a nil pointer)

Question 79MediumGo 1.22 to 1.25
package main

import "fmt"

type T struct{}

func main() {
    var p *T = nil
    var i any = p
    var j any

    fmt.Println(p == nil) // ?
    fmt.Println(i == nil) // ?
    fmt.Println(j == nil) // ?
    fmt.Printf("%T %v\n", i, i)
}

Output:

true
false
true
*main.T <nil>

i has dynamic type *T and a dynamic value of nil. The type word is set, so i != nil. An interface equals nil only when its type and value words are both nil. j was never assigned, so both words are nil.

To test whether the value inside is nil, use a type assertion (v, ok := i.(*T); ok && v == nil) or reflection (reflect.ValueOf(i).IsNil(), which panics for kinds that cannot be nil). The interviewer is checking that you can explain this from the two-word representation instead of just remembering the rule.

More on Interfaces, Methods & Embedding

All 35 Interfaces, Methods & Embedding questions