4 ms·
>As golang has no instanceof or typeof keyword, you practically you cannot use a method that uses multiple types as arguments without having to use the reflect
by kgeist 4y ago
>As golang has no instanceof or typeof keyword, you practically you cannot use a method that uses multiple types as arguments without having to use the reflect API
You don't have to use reflection, there are typecasts:
"value, ok := arg.(MyType)"
You can also switch on a type. How is it different from instanceof in practice?
- cookiengineer 4y agoThe point of described scenario was to have a _different_ if/elseif branch, and not the identical behaviour for one struct type. If item instanceof HouseStruct {} Elseif item instanceof ContainerStruct {} With your suggested solution you end up with OOP fatigue and GenericBuildingImplementationStruct which is probably not what golang wanted to embrace with their type system.
- kgeist 4y agoI'm not sure I follow what you mean. In Go the equivalent to your example is: switch val := item.(type) { case HouseStruct: case ContainerStruct: }
- erik_seaberg 4y agoDoesn’t type switch require an interface value? Go tries not to add boxing-like overhead to generics.
- deleted 4y ago[deleted]