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/f1981166-b7e0-4719-ae96-baf02534a639n%40googlegroups.com.

Reply via email to