Thanks a lot for the thoughts everyone, I will take all your suggestions 
under advisement.

Jan:
Thanks for the tips for performance, however with this project I'm not 
particularly trying to squeeze as much performance as possible, and I 
prefer the "safety" of the otherway.
I mean safe in that, with your suggestion you would have the "nonsense" 
value still accessible, even if I really shouldn't touch it because the 
bool is "false".
Maybe it's me being lazy, but I much prefer the compiler not letting me 
mess with something that I shouldn't (I'm very jealous of Rust's 
"container" Mutexes).

On Thursday, June 17, 2021 at 1:12:39 PM UTC+1 Brian Candler wrote:

> On Thursday, 17 June 2021 at 09:02:48 UTC+1 axel.wa...@googlemail.com 
> wrote:
>
>>
>>> How would you go about modelling this type in Go?
>>>
>>
>> Either using an interface, or by having two pointer-fields and only 
>> setting one.
>>
>
> I'd just add for clarity: in Go an interface value is also something 
> inherently "nillable", so lets you test for the presence/absence of a 
> value, like a pointer does.
>
> If you do this, then you can switch on the type of the value - so you 
> don't need to keep a separate field for "type".  But probably the most 
> persuasive argument is if your actions share some common behaviour, such as 
> "Perform this action" or "Describe this action".  Those sit very nicely as 
> methods on the interface.
>
> A benefit of pointers is that it's very easy to serialize and deserialize 
> as an optional field (to JSON, say), without any extra work.  You can also 
> do it with interfaces, and you get more control, but you need to do some 
> plumbing.  This looks like a nice overview: 
> http://gregtrowbridge.com/golang-json-serialization-with-interfaces/
>

-- 
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/44bd58eb-205b-40f2-963c-028d8e55a81fn%40googlegroups.com.

Reply via email to