3 ms·
Because 0 has a "default type" of int. So when the compiler goes to determine the type of x, it sees its set to 0, which the compiler assumes to be an int. I'v
by dhagz 6y ago
Because 0 has a "default type" of int. So when the compiler goes to determine the type of x, it sees its set to 0, which the compiler assumes to be an int.
I've found thinking about nil too much in Go hurts or creates weird loops in my brain, so I just tend to worry about it when I'm dealing with interfaces or pointers. Everything else I'm looking at zero-values.
- stcredzero 6y agoI'd much prefer it if in golang, the type of nil was some "nilType" for which != and == is defined for all other types. Or, define some other operation, like isNil() Then determining if something is nil could be done without thinking about it at all. nil would be nil would be nil. (Also, it would be illegal to put methods on nilType, of course.) (Yes, I make my living writing go, and this would make my life easier. And no, in that case, don't allow x := nil.)