A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://yourbasic.org/golang/gotcha-why-nil-error-not-equal-nil/ below:

Nil is not nil · YourBasic Go

Nil is not nil

yourbasic.org/golang

Why is nil not equal to nil in this example?

func Foo() error {
	var err *os.PathError = nil
	
	return err
}

func main() {
	err := Foo()
	fmt.Println(err)        
	fmt.Println(err == nil) 
}
Answer

An interface value is equal to nil only if both its value and dynamic type are nil. In the example above, Foo() returns [nil, *os.PathError] and we compare it with [nil, nil].

You can think of the interface value nil as typed, and nil without type doesn’t equal nil with type.

If we convert nil to the correct type, the values are indeed equal.

…
fmt.Println(err == (*os.PathError)(nil)) 
…
A better approach

To avoid this problem use a variable of type error instead, for example a [named return value](/golang/named-return-values-parameters/).

func Foo() (err error) {
	
	return 
}

func main() {
	err := Foo()
	fmt.Println(err)        
	fmt.Println(err == nil) 
}

Best practice: Use the built-in error interface type, rather than a concrete type, to store and return error values.

See Interfaces in 5 easy steps for an extensive guide to interfaces in Go.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4