将结构体从接口转换回其数据类型,并运行其方法。

huangapple go评论93阅读模式
英文:

Get you struct back to it's data type after passing it as an interface and run it's methods?

问题

我有一个如下所示的结构体:

type MyStruct struct {
   EmbeddedFooBar
}

func (m *MyStruct) Foo(b *http.Request) {
   // 做一些操作
} 

func fn(args ...interfaces) {
    // 在这里我想要获取我的结构体并运行 "Get" 方法
    // 请记住,我也要传递一个指针参数到结构体方法中
    strt := args[0]
    
    ....
    // 将结构体转换回静态数据类型 MyStruct
    // 并运行 "Get()" 方法,不要在意我将如何/在哪里获取 *http.Request 来传递,假设我可以获取到
    ....
    
    strt.Get(*http.Request)
}

func main() {
    a := &MyStruct{}
    fn(a)
}

我将上述结构体传递给一个可变参数函数 fn,该函数期望 ...interfaces{}(因此任何类型都可以满足参数)。

在函数 fn 内部,我想要从接口 args[0] 中获取回我的结构体 MyStruct,并运行它的方法 Get,该方法也可以接受像 *http.Request 这样的接收器。

如何从接口 arg[0] 中获取我的结构体,并以能够传递指针的方式运行结构体的 Get 方法?

英文:

I have a struct as follows

type MyStruct {
   EmbeddedFooBar
}

func (m *MyStruct) Foo(b *http.Request) {
   // Doing something
} 
    
func fn(args ...interfaces) {
    // It's here I want to get my struct back and run the "Get" method
    // Please keep in mind I am too pass a pointer param into the struct method
    strt := args[0]
    
    ....
    get struct back to static data type MyStruct
    and run "Get()", dont mind how/where I will get *http.Request to pass, assume I can
    ....

    strt.Get(*http.Request)
}

func main() {
    a := &MyStruct{}
    fn(a)
}

I am passing the struct above to a variadic function fn that expects ...interfaces{} (thus any type can satisfy the params)

Inside the function fn I want to get back my struct MyStruct to it's data type and value and run it's method Get that can also accept receivers such as *http.Request

How do I get My Struct back from the interface arg[0] and run the method Get of the struct with the ability of passing a pointer.

答案1

得分: 1

你想要的是类型断言。解决方案可能如下所示:

func fn(args ...interface{}) {
    if strt, ok := args[0].(*MyStruct); ok {
        // 使用结构体
    } else {
        // 出现了错误
    }
    // .......
}
英文:

What you want is Type Assertion. Solution could something like this:

func fn(args ...interfaces) {
    if strt, ok := args[0].(*MyStruct); ok {
        // use struct
    } else {
        // something went wrong
    }
    // .......
}

答案2

得分: 0

看起来你想要的是一个特定的接口,具体来说是一个接受指向 http.Request 的指针的 Get 方法。

例如:

type Getter interface {
    Get(*http.Request)
}

func fn(getters ...Getter) {
    getter := getters[0]
    getter.Get(*http.Request)
}

func main() {
    a := &MyStruct{}
    fn(a)
}

这样可以让编译器检查 fn 的参数是否 恰好 拥有你所需的方法,但它不需要知道类型的其他信息。在这里可以阅读更多关于接口的内容:链接

英文:

It sounds like what you want here is a specific interface, specifically one that has a Get method that accepts a pointer to an http.Request.

For example:

type Getter interface {
    Get(*http.Request)
}

func fn(getters ...Getter) {
    getter := getters[0]
    getter.Get(*http.Request)
}

func main() {
    a := &MyStruct{}
    fn(a)
}

This allows the compiler to check that the arguments to fn have exactly the methods you need, but it doesn't need to know anything else about the type. Read more about interfaces here.

huangapple
  • 本文由 发表于 2015年7月15日 07:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/31419128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定