doupu1727 2017-02-17 17:07
浏览 119
已采纳

Go函数类型,该函数类型返回与接口一起使用的结构

I have a struct in a package that has time consuming methods on it and is also time consuming to construct via it's factory function. Therefore, in the package that depends on this other struct I would like to be able to test it using both a fake factory function and a fake struct once it has been created. As the struct is constructed via a factory function I would like to fake the factory function and pass an alternative factory function into my struct during testing time.

An example of the expensive package would be:

package expensive

import "fmt"

type myStruct struct{}

func (m *myStruct) DoSomething() {
    fmt.Println("In Do something")
}

func (m *myStruct) DoSomethingElse() {
    fmt.Println("In do something else")
}

// CreateInstance is expensive to call
func CreateInstance() *myStruct {
    return &myStruct{}
}

My main package that uses this then looks like this:

package main

import "play/expensive"

func main() {
    thing := structToConstruct{expensive.CreateInstance}
    thing.performAction()
}

type myInterface interface {
    DoSomething()
}

type structToConstruct struct {
    factoryFunction func() myInterface
}

func (s *structToConstruct) performAction() {
    instance := s.factoryFunction()
    instance.DoSomething()
}

However, this code complains with the error:

.\main.go:6: cannot use expensive.CreateInstance (type func() *expensive.myStruct) as type func() myInterface in field value

However, *expensive.myStruct does implement the myInterface interface so I do not understand why Go is complaining about the type safety of this setup.

I've since realised after @jmaloney guideance that I could just wrapper my function like this in my main method:

    wrapper := func() myInterface {
        return expensive.CreateInstance()
    }
    thing := structToConstruct{wrapper}

and this then works but I still don't really understand why I can't use a struct that implements an interface when a function is expecting an instance of that interface to be returned especially when no type assertion/conversion is necessary on this fix as it is simply just calling the underlying factory function.

EDIT: I've since come across this proposal to add this to the language. The proposal was rejected:

https://github.com/golang/go/issues/12754

  • 写回答

2条回答 默认 最新

  • douweilaton2762 2017-02-17 17:15
    关注

    getInstance needs to return myInterface

    package main
    
    import "fmt"
    
    func main() {
        var function func() myInterface
    
        function = getInstance
    
        newSomething := function()
    
        newSomething.doSomething()
    }
    
    type myInterface interface {
        doSomething()
    }
    
    type myStruct struct{}
    
    func (m *myStruct) doSomething() {
        fmt.Println("doing something")
    }
    
    func getInstance() myInterface {
        return &myStruct{}
    }
    

    Playground example

    However, *expensive.myStruct does implement the myInterface interface so I do not understand why Go is complaining about the type safety of this setup.

    In that instance you were not dealing with Go's interfaces you were dealing with the type signature of your struct.

    when you first declared your struct with factoryFunction func() *myFunction factoryFunction now always needs to match the declared signature.

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

报告相同问题?

悬赏问题

  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开