Junior — Middle
What types of data are allowed to be compared with Nil in programming?
sobes.tech AI
Answer from AI
In Go, the value nil can be compared with variables that have types allowing nil values.
These types include:
- Pointers
- Slices
- Maps
- Channels
- Interfaces
- Functions
For example, you can check if a pointer is nil to determine if it has been initialized:
var p *int
if p == nil {
fmt.Println("p is nil")
}
For basic types such as int, bool, struct, comparison with nil is not allowed and will result in a compile-time error.