In
Golang, nil check is frequently seen in GoLang code especially for error check. In most cases,
nil check is straight forward, but in interface case, it's a bit different and special care needs to be taken.
Here the task is to check pointer or interface is nil or not in Golang, you can check with the following:
Example 1: In this example, the pointer is checked whether it is a nil pointer or not.
Output:
pnt is a nil pointer: true
pnt1 is a nil pointer: false
Example 2: In this example, the interface is checked whether it is a nil interface or not.
Output:
t is a nil interface: true
t is a nil interface: false
Example 3: In this example, the interface holding a nil pointer is checked whether it is a nil interface or not.
Output:
val2 is a nil interface: false
val2 is a interface holding a nil pointer: true