How to Check nil Interface in Golang?

Simplify Complexity
2 min readMay 15, 2022

It’s not a good idea to check an interface for nil.

Try the following code:

type user interface{}
type staff struct{}
func compareNil() {
var generic user
generic = nil
// works as expected
fmt.Printf("value=%v type=%T (generic==nil)=%v\n", generic, generic, generic == nil)

--

--

Simplify Complexity

Golang, Distributed Systems, File Systems, Python, C/C++, Linux