Notice that if you don't assign to `tok`, multiple alternatives in `case` 
works.

But the issue is that in Go there are no "duck typing" for data, only for 
methods (interfaces).  `xml.StartElement` and `xmlEndElement` not being the 
same type, the assignment ('tok.name.Local = "types"`) won't work.

Would converting to a method,  something like this 
<https://go.dev/play/p/Y4gxPr9HxcZ>, work for you  ? It could be changed by 
making the interface "Named" the type to match in the `switch` clause. 
There are many alternatives, the key point is when you are trying to share 
functionality (or data) across different types, it is usually done using 
common interfaces.


On Friday, August 25, 2023 at 4:44:02 PM UTC+2 محمد بومنذر wrote:

> Greetings everyone,
>
> I'm writing a parser based on Go's `encoding/xml`, I tried to use the 
> following pattern but it was deemed invalid:
>
> ```
> switch tok := p.tok.(type) {
> case xml.StartElement, xml.EndElement:
> tok.Name.Local == "types"
> }
>
>
> ```
> To me, it'd make sense if it was allowed since both types are structs and 
> both of them share the field `Name` with the subsequent field `Local`.
>

-- 
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/d2c96949-155a-43e5-9fc3-1a294f59e31cn%40googlegroups.com.

Reply via email to