Best practice for unions in Go

前端 未结 4 1514
一个人的身影
一个人的身影 2021-01-17 11:11

Go has no unions. But unions are necessary in many places. XML makes excessive use of unions or choice types. I tried to find out, which is the preferred way to work around

4条回答
  •  野的像风
    2021-01-17 11:52

    I think this amount of code might be reduced, e.g. I personally do not think that safeguarding type Misc against containing "illegal" stuff is really helpful: A simple type Misc interface{} would do, or?

    With that you spare the constructors and all the Is{Comment,ProcessingInstruction,WhiteSpace} methods boil down to a type switch

    switch m := misc.(type) {
        Comment: fmt.Println(m)
        ... 
        default: panic()
    }
    

    Thats what package encoding/xml does with Token.

提交回复
热议问题