doouzlrvb01417498 2018-04-20 08:15
浏览 47
已采纳

Golang,带有指针和接口的用例

I am writing a library I'll need to use in others parts of my programs, for this I had to use a lot interfaces and I ended up in a case I don't know how to solve.

Here is my code :

main.go

package main

import (
    "test/bar"
)

// Foo is defined in another package
type Foo struct {
    WhatWeWant string
}

func (f *Foo) getResult() interface{} {
    return f.WhatWeWant
}

func newFoo() interface{} {
    return &Foo{
        WhatWeWant: "test",
    }
}

func main() {
    bar := bar.Bar{
        NewFooer: newFoo,
    }
    bar.Execute()
}

bar/bar.go

package bar

import (
    "fmt"
    "reflect"
)

type myInterface interface {
    getResult() interface{}
}

type Bar struct {
    NewFooer func() interface{}
}

func (b *Bar) Execute() {
    foo := b.NewFooer()

    // executeLib only accept pointer
    executeLibWrapper(foo)

    whatWeWant := foo.(myInterface).getResult()
    fmt.Println(whatWeWant)
    fmt.Println("Win!")

}

// This function is executed in an external library I have no control on
func executeLibWrapper(src interface{}) {
    executeLib(reflect.TypeOf(src))
}

func executeLib(src reflect.Type) {

    if src.Kind() == reflect.Ptr {
        executeLib(src.Elem())
        return
    }

    switch src.Kind() {
    case reflect.Struct:
        fmt.Println("Struct OK!")
    default:
        panic(fmt.Sprintf("Can't detect struct, we detect %s", src.Kind()))
    }
}

I get the error

panic: interface conversion: *main.Foo is not bar.myInterface: missing method getResult

My goal is to be able to call getResult() after executing the library. Here is a playground : https://play.golang.org/p/7G2wc6uGngH. This playground works, so there is a strong possibilities that the problem come from the fact it is in different packages.

Note that I need to pass a pointer to executeLib, I can't get the pointer in execute() because otherwise I'll lose the foo type and can't execute the library : https://play.golang.org/p/A8ETfuMQyQB. This is why I have to return the pointer in newFoo()

Thank you for your help !

  • 写回答

2条回答 默认 最新

  • drby30217 2018-04-20 12:54
    关注

    If you want to share an interface and their funcs across packages you have export the interface and its funcs:

    type MyInterface interface {
        GetResult() interface{}
    }
    

    When you change the implementation of Foo

    func (f *Foo) GetResult() interface{} {
        return f.WhatWeWant
    }
    

    and the call

    whatWeWant := foo.(MyInterface).GetResult()
    

    it compiles and executes without panic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛