duanpingzu7194 2017-07-16 23:14
浏览 147
已采纳

如何使用反射将值设置为包含nil的指针

I'm trying to set a value to a nil pointer in a struct like so.

// https://play.golang.org/p/jPTMNC_ZQ9
package main

import (
    "fmt"
    "reflect"
)

type T struct {
    A *int
}

func main() {
    fmt.Println("Hello, playground")
    t := &T{}

    v := 1
    vptr := &v

    CopyValue(vptr, t.A) // I want to set t.A to contain 1
}

func CopyValue(src interface{}, dest interface{}) {
    srcRef := reflect.ValueOf(src)
    if srcRef.Kind() == reflect.Ptr {
        srcRef = srcRef.Elem()
    }

    destRef := reflect.New(srcRef.Type()).Elem()
    destRef.Set(srcRef)
    reflect.ValueOf(dest).Elem().Set(destRef)
}

However, I encounter the following error:

panic: reflect: call of reflect.Value.Set on zero Value

goroutine 1 [running]:
reflect.flag.mustBeAssignable(0x0, 0x1040a128)
    /usr/local/go/src/reflect/value.go:221 +0x260
reflect.Value.Set(0x0, 0x0, 0x0, 0xdefc0, 0x1040a128, 0x182)
    /usr/local/go/src/reflect/value.go:1339 +0x40
main.CopyValue(0xd7860, 0x1040a124, 0xd7860, 0x0)
    /tmp/sandbox487854080/main.go:30 +0x1a0
main.main()
    /tmp/sandbox487854080/main.go:19 +0x100

What am I doing wrong?

  • 写回答

3条回答 默认 最新

  • doujiao1814 2017-07-17 03:06
    关注

    In order to be able to modify what t.A points to, you need to send a reference to it to your CopyValue function.

    CopyValue(vptr, &t.A) // (note the &)
    

    You can then assign the pointer to the new address:

    func CopyValue(src interface{}, dest interface{}) {
        srcRef := reflect.ValueOf(src)
        vp := reflect.ValueOf(dest)
        vp.Elem().Set(srcRef)
    }
    

    See the 3rd "law of reflection" here: https://blog.golang.org/laws-of-reflection

    Full working code:

    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    type T struct {
        A *int
    }
    
    func main() {
        t := &T{}
        v := 1
        vptr := &v
        CopyValue(vptr, &t.A) // we pass a reference to t.A since we want to modify it
        fmt.Printf("%v
    ", *t.A)
    }
    
    func CopyValue(src interface{}, dest interface{}) {
        srcRef := reflect.ValueOf(src)
        vp := reflect.ValueOf(dest)
        vp.Elem().Set(srcRef)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?