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
How to set timeout for http.Get() requests in Golang?
Find length of Channel, Pointer, Slice, String and Map
Regular expression to validate the date format in "dd/mm/yyyy"
Example: Split, Join, and Equal from BYTES Package
How to use wildcard or a variable in our URL for complex routing?
Copy an array by value and reference into another array
Golang String Concatenation
Select single argument from all arguments of variadic function
How to extract text from between html tag using Regular Expressions in Golang?
How to check if a string contains certain characters in Golang?