drm16022 2017-09-21 11:01
浏览 64
已采纳

在带有反射的接口下更改指针类型和值

Is it possible to change pointer type and value of variable defined by interface?

I can change pointer value with reflection: v.Elem().Set(reflect.ValueOf(&Greeter{"Jack"}).Elem()) which is equivalent to a = &Greeter{"Jack"}.

But how can I make a reflection equivalent for a = &Greeter2{"Jack"}?

UPDATE: Unfortunately in real problem that I'm trying to solve I can not address original variable (panic: reflect: reflect.Value.Set using unaddressable value), that's why I'm trying to workaround at pointer level.

Here is full example code:

package main

import (
    "fmt"
    "reflect"
)

type Greeter struct {
    Name string
}

func (g *Greeter) String() string {
    return "Hello, My name is " + g.Name
}

type Greeter2 struct {
    Name string
}

func (g *Greeter2) String() string {
    return "Hello, My name is " + g.Name
}

func main() {
    var a fmt.Stringer
    a = &Greeter{"John"}

    v := reflect.ValueOf(a)
    v.Elem().Set(reflect.ValueOf(&Greeter{"Jack"}).Elem())
    //v.Elem().Set(reflect.ValueOf(&Greeter2{"Jack"}).Elem()) // panic: reflect.Set: value of type main.Greeter2 is not assignable to type main.Greeter
    fmt.Println(a.String()) // Hello, My name is Jack

    a = &Greeter2{"Ron"}
    fmt.Println(a.String()) // Hello, My name is Ron
}
  • 写回答

1条回答 默认 最新

  • douxihui8270 2017-09-21 11:09
    关注

    Everything in Go is passed by value. Interfaces too. When you pass a value of interface type, a copy of the interface value will be made (along with the (value;type) inside it), and you will only be able to modify the copy which will not affect the original.

    You have a variable a of interface type. If you want to modify the value stored in this variable, you have to pass / use the address of this variable.

    First let's modify the Greeter2.String() method to know which one gets called:

    func (g *Greeter2) String() string {
        return "Hello2, My name is " + g.Name
    }
    

    And also call a.String() after first init to see that it originally does contain a *Greeter value as we'll soon change it.

    var a fmt.Stringer
    a = &Greeter{"John"}
    fmt.Println(a.String()) // Hello, My name is John
    
    v := reflect.ValueOf(&a).Elem()
    v.Set(reflect.ValueOf(&Greeter2{"Jack"}))
    fmt.Println(a.String()) // Hello2, My name is Jack
    
    a = &Greeter2{"Ron"}
    fmt.Println(a.String()) // Hello2, My name is Ron
    

    Output (try it on the Go Playground):

    Hello, My name is John
    Hello2, My name is Jack
    Hello2, My name is Ron
    

    So essentially there is no such thing as changing the value and type of an interface value, only changing the data in a variable (that has a memory address) that may be of interface type.

    So the key things were:

    1. Start from an address: reflect.ValueOf(&a)
    2. And you have to set a pointer value into it as only *Greeter2 implements fmt.String, but not Greeter. See Go, X does not implement Y (... method has a pointer receiver) for details.

    For more details and in-depth introducation, read: The Go Blog: The Laws of Reflection

    Edit: about modifying a copy

    First section of my answer: Whenever you pass something a copy is made. This also includes passing an interface value to reflect.ValueOf(): that will also receive a copy, so you can't avoid this. Only way to make this work is to pass a pointer; because the pointer will also be copied, but we're modifying the pointed value (not the pointer) which is the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退