I've never really been at peace with using zero values to indicate that a 
struct field is absent. I use nil (fieldA *int, fieldB *string, etc.) if I 
have to (I'm looking at you, JSON), but in most other cases the "nil" 
approach feels dangerous (panics) and magical.

Does anyone have an approach they feel comfortable with? At various points 
I've thought about:

   - bool guard (HasField bool, Field int)
   - pointer (Field *int)
   - zero value (Field int, absent if 0)
   - magic value (Field int, absent if MaxInt)
   - private values (func Field() (int, bool))
   - enum guard (Mode StructMode, Field int)
   - generic optional (Field Optional[int])

I prefer idiomatic approaches but they are all error prone because checking 
the guard is not required programmatically. I generally use the bool guard 
and sometimes pointer. I feel like I should be more comfortable with the 
zero value but it doesn't always work.

What say you, oh Go Wizards?

-- Salvatore
smile.



-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/82a3c981-8c19-49d5-96e1-865747e7946fn%40googlegroups.com.

Reply via email to