In null-supporting languages I’d use null worth because the indicator that worth is just not obtainable:
class Consumer {
age: Int? = null
}
What’s equal in Go? Ought to I take advantage of boolean flag?
kind Consumer struct {
isAgePresent bool
age int
}
If reply is sure, then what are the rules, naming conventions, and so forth.
I’d use a pointer.
kind Consumer struct {
Age *int
}
the zero worth for a pointer is nil
.
Three widespread practices exist to point the dearth of a price.
- zero worth
- nil pointer
- false boolean worth
If the zero worth is a sound worth, than the non-zero the zero worth method is easiest, as a result of it requires nothing, however isn’t viable if the zero worth in your kind is a sound worth.
The nil pointer method works with the potential draw back of heap allocation.
The boolean method is used the place the zero worth is a sound worth however heap allocation isn’t essentially fascinating. Numerous libraries use this method, together with database/sql. This method has the advantage of readability at the price of verbosity. That’s, it’s apparent to readers that the worth is legitimate or current or not with out having to make any inferences primarily based on if the pointer is nil or the zero worth.