Reputation: 722
While organizing my projects, I observe nil comparisons in many places. I wish to replace nil
with NULL
or Null
. I respect Golang specs, but I am curious if we can do this.
I already did it for interface{}
, context.Context
as follows.
type CON = context.Context
type Any = interface{}
Upvotes: 0
Views: 396
Reputation: 8395
You cannot. What you show there are type aliases. nil
is not a type. It is a value of a wide range of different types. You may hope that you can make a constant with a value of nil
, similar to how you can make a constant of value 0
, but this is explicitly disallowed by the compiler:
const NULL = nil
Error: const initializer cannot be nil
According to the language specification:
There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants.
None of these types can have a nil
value, therefore a nil
constant is not possible.
You might also try to make a variable which holds the value nil
, but you'll find the problem that it doesn't work if you don't declare the type of the variable:
var NULL = nil
Error: use of untyped nil
You can make it legal by adding a nil
able type to the variable, but then it will no longer be very useful as it will only be comparable to that specific type.
Upvotes: 4