go(golang)中,如何将接口指针转换为结构指针? [英] In go (golang), how can you cast an interface pointer into a struct pointer?

查看:2083
本文介绍了go(golang)中,如何将接口指针转换为结构指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一些需要指向结构体的指针的外部代码。在调用代码的时候,我有一个接口变量。



当创建一个指向该变量的指针时,当我需要它时,指针的类型是 interface {} * 成为结构类型的指针类型。



将代码映射到 TestCanGetStructPointer 不知道Cat class ,它存在于一些外部包中。



我怎样才能把它转换成这个?



以下是一个代码示例:

  import(
reflect
testing


类型Cat结构{
名称字符串
}

类型SomethingGeneric结构{
getSomething func()interface { }

$ b func getSomeCat()接口{} {
返回Cat {}
}

var somethingForCats = SomethingGeneric {getSomething: getSomeCat}
$ b $ func TestCanGetStructPointer(t * testing.T){
interfaceVariable:= somethingForCats.getSomething()

指针:=& interfaceVariable

inter faceVarType:= reflect.TypeOf(interfaceVariable)
structPointerType:= reflect.PtrTo(interfaceVarType)
pointerType:= reflect.TypeOf(指针)
$ b $如果pointerType!= structPointerType {
t.Errorf(指针类型为%v,但预期为%v,pointerType,structPointerType)
}

}
pre>

测试失败:

指针类型为* interface {},但是期待*参数化.Cat

解决方案

@ dyoo的例子确实有效,但它依赖于您手动cast Dog Cat



以下是一些一个令人费解的/详细的例子,它避免了这个限制:

  package main 

import(
fmt
反映


类型Cat结构{
名称字符串
}

类型SomethingGeneric结构{
getSomething func()接口{}
}

f unc getSomeCat()interface {} {
return Cat {name:Fuzzy Wuzzy}
}

var somethingForCats = SomethingGeneric {getSomething:getSomeCat}

func main(){
interfaceVariable:= somethingForCats.getSomething()
castVar:= reflect.ValueOf(interfaceVariable)
castVar.Convert(castVar.Type())

//如果你想要一个指针,可以这样做:
fmt.Println(reflect.PtrTo(castVar.Type()))

// deref'd val $如果castVar.Type()!= reflect.TypeOf(Cat {}){
fmt.Printf(类型为%v但预期为%v \ n,castVar,reflect.TypeOf(& Cat {}))
} else {
fmt.Println(castVar.Field(0))
}
}
pre>


I want to use some external code that requires a pointer to a struct. At the point that the code is called, I have an interface variable.

When creating a pointer off of that variable, the pointer's type is interface{}* when I need it to be the pointer type of the struct's type.

Image the code in TestCanGetStructPointer does not know about the Cat class, and that it exists in some external package.

How can I cast it to this?

Here is a code sample:

import (
    "reflect"
    "testing"
)   

type Cat struct {
    name string
}

type SomethingGeneric struct {
    getSomething func() interface{}
}

func getSomeCat() interface{} {
    return Cat{}
}

var somethingForCats = SomethingGeneric{getSomething: getSomeCat}

func TestCanGetStructPointer(t *testing.T) {
    interfaceVariable := somethingForCats.getSomething()

    pointer := &interfaceVariable

    interfaceVarType := reflect.TypeOf(interfaceVariable)
    structPointerType := reflect.PtrTo(interfaceVarType)
    pointerType := reflect.TypeOf(pointer)

    if pointerType != structPointerType {
        t.Errorf("Pointer type was %v but expected %v", pointerType, structPointerType)
    }

}

The test fails with:

Pointer type was *interface {} but expected *parameterized.Cat

解决方案

@dyoo's example does work, but it relies on you to manually cast Dog and Cat.

Here's a bit of a convoluted/verbose example which avoids that constraint somewhat:

package main

import (
    "fmt"
    "reflect"
)

type Cat struct {
    name string
}

type SomethingGeneric struct {
    getSomething func() interface{}
}

func getSomeCat() interface{} {
    return Cat{name: "Fuzzy Wuzzy"}
}

var somethingForCats = SomethingGeneric{getSomething: getSomeCat}

func main() {
    interfaceVariable := somethingForCats.getSomething()
    castVar := reflect.ValueOf(interfaceVariable)
    castVar.Convert(castVar.Type())

    // If you want a pointer, do this:
    fmt.Println(reflect.PtrTo(castVar.Type()))

    // The deref'd val
    if castVar.Type() != reflect.TypeOf(Cat{}) {
        fmt.Printf("Type was %v but expected %v\n", castVar, reflect.TypeOf(&Cat{}))
    } else {
        fmt.Println(castVar.Field(0))
    }
}

Playground Link

这篇关于go(golang)中,如何将接口指针转换为结构指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆