Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/golayrva/public_html/app/code/core/Mage/Core/Model/Layout.php on line 443
How to check pointer or interface is nil in Golang - Go Programming Language? - golangprograms.com

How to check pointer or interface is nil?


Example

package main
 
import (
    "fmt"
)
 
type Temp struct {
}
 
func main() {
    var pnt *Temp       // pointer
    var inf interface{} // interface declaration
    inf = pnt           // inf is a non-nil interface holding a nil pointer (pnt)
 
    fmt.Printf("pnt is a nil pointer: %v\n", pnt == nil)
    fmt.Printf("inf is a nil interface: %v\n", inf == nil)
    fmt.Printf("inf is a interface holding a nil pointer: %v\n", inf == (*Temp)(nil))
}

Output

pnt is a nil pointer: true
inf is a nil interface: false
inf is a interface holding a nil pointer: true
Most Helpful This Week