Sobes.tech
Junior — Senior

Behavior of an interface containing a nil pointer

livecode

Task condition

You need to understand what result the following program will output.

type I interface {
  Foo()
}

type S struct{}

func (s *S) Foo() {
  fmt.Println("foo")
}

func Build() I {
  var res *S
  return res
}

func main() {
  i := Build()
  if i != nil {
    i.Foo()
  } else {
    fmt.Println("nil")
  }
}