如何在GO中通过反射获取结构域的地址

时间:2019-02-06 06:10:05

标签: go serialization reflection

我正在研究将字节流反序列化为对象的代码,并且一直坚持获取结构字段的指针。

代码的基本工作原理如下:它获取结构的指针,然后根据将其序列化的类型(例如)。如果是整数,则占用接下来的4个字节。棘手的情况是,如果它是一个结构,因为我必须对其所有属性递归运行Deserialize,而且我不知道如何获取其字段的地址以将其传递给Deserialize。

func Deserialize(objPtr interface{}, b []byte) (bytesRead int) {
    // it should be the address of the object

    val := reflect.ValueOf(objPtr).Elem()
    valPtr := reflect.ValueOf(objPtr)

    // check if either the object or *object is Serializable
    _, isSerializable := (val.Interface()).(Serializable)
    _, bo := (valPtr.Interface()).(Serializable)
    isSerializable = isSerializable || bo

    // specific type serialization
    if isSerializable{
        return objPtr.(Serializable).Deserializebyte(b)
    }

    switch val.Kind() {
    case reflect.Uint32, reflect.Int, reflect.Int32:
        res := reflect.ValueOf(binary.LittleEndian.Uint32(b[:4]))
        valPtr.Set(res)
        return 4
    case reflect.Uint64, reflect.Int64:
        res := reflect.ValueOf(binary.LittleEndian.Uint32(b[:8]))
        valPtr.Set(res)
        return 8
    case reflect.Struct:
        n_bytes := 0
        for i := 0; i < val.NumField(); i++ {

            // stuck in here
            valPtr.Elem()

            // I don't think the next line works
            last_n_bytes := Deserialize(&(valPtr.Elem().Field(i).Interface()), b)
            n_bytes += last_n_bytes
            b = b[last_n_bytes:]
        }
        //valPtr.Set(res)
        return n_bytes
    default:
        panic("this panic is for debug, every case should be handled above")
        res := val.Bytes()
        valPtr.Set(res)
        return len(val.Bytes())
    }
    return 0
}

1 个答案:

答案 0 :(得分:2)

使用反射API来获取字段的address

last_n_bytes := Deserialize(valPtr.Elem().Field(i).Addr().Interface(), b)

superint实例会出现紧急情况,因为应用程序通过反射API获取了未导出字段的地址。不允许这样做,因为它将允许另一个程序包修改该字段。

这是一个包含exported字段的有效示例:

type superint struct {
    A int
    B int
}

func (s *superint) lol() {}

type a interface{ lol() }

func main() {
    i := superint{A: 1, B: 9}
    valPtr := reflect.ValueOf(&i)
    fmt.Printf("%v \n", &i.A)
    fmt.Printf("%v \n", valPtr.Elem().Field(0).Addr().Interface())
}