On Jun 17, 2021, at 1:20 AM, Jan Mercl <0xj...@gmail.com> wrote: > > On Thu, Jun 17, 2021 at 9:43 AM Joshua <joshua.oconno...@gmail.com> wrote: > >> 1) I'm modelling a data type which has a field which may or may not be >> there, would most Gophers reach for a pointer here, and use `nil' to >> represent a missing value? > > That's the usual approach seen in the wild and IMO often the wrong one. > > Unless the size of the field's type is big, I'd suggest just a plain > field and a boolean value that represents the "present/valid" > information.
This can get error prone as you may forget to update the validity field and the error may not be caught easily. And painful if you have more than one optional field. Now you need to invent more names or use longer names (foo_valid). The pointer alternative will fail more obviously if you mess up! > > Less GC pressure, improved cache locality. > > -- > 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/CAA40n-XGP2edWe1b13kpE4PhdFdCsJWGk79z8ngEpj19ycwmxQ%40mail.gmail.com. -- 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/1CBA5F6B-2570-44A4-A6F0-26679FA1A43F%40iitbombay.org.