登录
首页 >  Golang >  Go问答

GO中如何通过reflect获取struct字段的地址

来源:stackoverflow

时间:2024-03-20 18:30:32 351浏览 收藏

在 Go 语言中,可以通过 `reflect` API 获取结构体字段的地址。通过 `valPtr.Elem().Field(i).Addr().Interface()` 可以获取第 `i` 个字段的地址,然后将其传递给反序列化函数,从而实现结构体的反序列化。需要注意的是,如果字段是未导出的,则无法通过反射 API 获取其地址。

问题内容

我正在编写将字节流反序列化为对象的代码,但我一直在获取结构体字段的指针。

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

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
}

解决方案


使用reflect 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())
}

好了,本文到此结束,带大家了解了《GO中如何通过reflect获取struct字段的地址》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>