我是一个有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:
错误信息:
在golang中,我尝试实现一个在不同包中定义的接口时遇到了一些问题。下面是我对问题的最小重现:
接口:
package interfaces
type Interface interface {
do(param int) int
}
实现:
package implementations
type Implementation struct{}
func (implementation *Implementation) do(param int) int {
return param
}
Main.go:
package main
import (
"test/implementing-interface-in-different-package/implementations"
"test/implementing-interface-in-different-package/interfaces"
)
func main() {
var interfaceImpl interfaces.Interface
interfaceImpl = &implementations.Implementation{}
}
错误信息:
test/implementing-interface-in-different-package
./main.go:10:16: cannot use implementations.Implementation literal (type
implementations.Implementation) as type interfaces.Interface in assignment:
implementations.Implementation does not implement interfaces.Interface (missing interfaces.do method)
have implementations.do(int) int
want interfaces.do(int) int
能否从不同的包中实现接口?
谢谢!
interface
的 API 可以重写为接受函数类型。请参见 https://stackoverflow.com/a/63557675/12817546。 - user12817546