modern-go / reflect2

reflect api without runtime reflect.Value cost
Apache License 2.0
758 stars 73 forks source link

can't work with interface var #7

Open nowind opened 5 years ago

nowind commented 5 years ago

code as following failed: var m interface{}=[]int{1,2} slType:=reflect2.TypeOf(m).(reflect2.SliceType) slType.LengthOf(m) //<--here i can't use &m

whisper-bye commented 5 years ago

+1

type message struct {
        crc uint16
}

msg := &message{}
unpack(b, msg)

func unpack(b []byte, msg interface{}) error {
    buf := bytes.NewBuffer(b)
    typ := reflect2.TypeOf(msg)
    structType := typ.(*reflect2.UnsafeStructType) // panic
...
whisper-bye commented 5 years ago

I fingure it out, it should be reflect2.TypeOfPtr(msg).Elem()