Skip to Content
marmot
DiagnosticsError codes

A use dominated by a nil check is not reported.

The mistake and the fix

The mistake
func find(id int) (*User, error) {
	if id == 0 {
		return nil, nil
	}
	...
}

u, err := find(id)
if err != nil {
	return err
}
fmt.Println(u.Name) // u is nil for id 0
Fixed
u, err := find(id)
if err != nil {
	return err
}
if u == nil { 
	return errNotFound 
}
fmt.Println(u.Name) 

The fix

Check for nil before the use, or make the function return an error instead of nil, nil.

See also all error codes and marmot explain.

On this page

Was this page helpful?

Report an issueOpen issues