我怎样才能使这种基于反射的 GO 代码更简单?

How can I make this reflect based GO code simpler?

我正在使用一个非常复杂的协议对一个相当复杂的结构进行编码,该协议混合了 ASN 和 XDR 的变体以及其他编码。

我基于 github 上可用的 xdr 编码器实现。该代码是基于反射的并且可以工作,但我不喜欢我实现目标类型开关的方式:

st := ve.Type().String()
    switch st {
    case "time.Time":

我认为以下方法可能更好,但我无法使其正常工作:

switch ve.(type) {
case time.Time:

它不起作用的原因是 ve 是相同的反射类型而不是目标类型。

以下函数提供了代码的完整上下文:

func (enc *encoderState) encode(v reflect.Value) {

ve := enc.indirect(v)

st := ve.Type().String()
switch st {
case "time.Time":
    log.Println("Handling time.Time")
    t, ok := ve.Interface().(time.Time)
    if !ok {
        enc.err = errors.New("Failed to type assert to time.Time")
        return
    }
    enc.encodeTime(t)
    return
case "[]uint8":
    log.Println("Handling []uint8")
    enc.writeOctetString(ve.Bytes())
    return
default:
    log.Printf("Handling type: %v by kind: %v\n", st, ve.Kind())
}

// Handle native Go types.
switch ve.Kind() {
case reflect.Uint8: // , reflect.Int8
    enc.writeUint8(uint8(v.Uint()))
    return
case reflect.Uint16: // , reflect.Int16
    enc.writeUint16(uint16(v.Uint()))
    return
case reflect.Bool:
    enc.writeBool(ve.Bool())
    return
case reflect.Struct:
    enc.encodeStruct(ve)
    return
case reflect.Interface:
    enc.encodeInterface(ve)
    return
}

// The only unhandled types left are unsupported.  At the time of this
// writing the only remaining unsupported types that exist are
// reflect.Uintptr and reflect.UnsafePointer.
enc.err = errors.New(fmt.Sprintf("unsupported Go type '%s'", ve.Kind().String()))

}

如果您知道更好的可以按类型和种类切换的示例,请告诉我。

谢谢

更新

阅读解决方案后,我调整了一个可行的变体:

vi := ve.Interface()
switch st := vi.(type) {
case time.Time:
    enc.encodeTime(vi.(time.Time))
    return
case []uint8:
    enc.writeOctetString(vi.([]byte))
    return
default:
    log.Printf("Handling type: %v by kind: %v\n", st, ve.Kind())
}

使用 type switch on the underlying value:

switch v := ve.Interface().(type) {
case time.Time:
    log.Println("Handling time.Time")
    enc.encodeTime(v)
    return
case []byte:
    log.Println("Handling []uint8")
    enc.writeOctetString(v)
    return
case byte:
    enc.writeUint8(v)
    return
// ... and more types here
default:
    log.Printf("Handling type: %v by kind: %v\n", ve.Type(), ve.Kind())
}

playground example

您也可以打开 reflect.Type 而不是字符串:

switch ve.Type() {
case reflect.TypeOf(time.Time{}):
    log.Println("Handling time.Time")
    ...
case reflect.TypeOf([]byte{}):
    log.Println("Handling []uint8")
    ...
case reflect.TypeOf(uint8(0)):
    ...
}

playground example