Go: nil == nil is true or false? Na tum jano na hum…

var a *string = nil    
var b interface{} = a
fmt.Println("a == nil:", a == nil) // true
fmt.Println("b == nil:", b == nil) // false
fmt.Println("a == b: ", a == b ) // true
var a int = 12
var b int = 12
c:= 12
fmt.Println("a == b :", a == b) // true
fmt.Println("a == c :", a == c) // true
var a *string = nil
var b interface{} = nil
fmt.Println("a == nil:", a == nil) // true
fmt.Println("b == nil:", b == nil) // true
fmt.Println("a == b:", a == b) // false (even though the value is
nil for both a and b)
use of untyped nil
var a *string = nil      // a is <*string, nil>
var b interface{} = nil // b is <nil, nil>
fmt.Println("a == nil:", a == nil) // true
fmt.Println("b == nil:", b == nil) // true
fmt.Println("a == b:", a == b) // false <*string,nil>!=<nil, nil>

Now come back to our main example

var a *string = nil     // a is <*string, nil>
var b interface{} = a
fmt.Println("a ==nil:",a==nil)//true(<*string, nil>==<*string, nil>)
fmt.Println("b == nil:", b == nil) //false (<*string,nil>==<nil,nil> fmt.Println("a == b: ", a == b ) // true
var b interface{} = a

Conclusion

This issue occurs frequently in industrial code like below code snippet is very common in Go where we can face such error.

var resp string
var err error
resp, err = CallFunction()
if err != nil{
// code inside this if statement will be executed if return type of CallFunction doesn't match with error
}
func CallFunction() custom.Error{
// return pointer variable
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store