目前,我仍然坚持使用此代码:https://play.golang.org/p/r_HEVmpOuD
package main
import "fmt"
type (
Collection struct {
Id string
}
CollectionInterface interface {
Process(...string)
}
)
func (this *Collection) Process(params ...string) {
this.Id = "ok"
}
func testfunc(input interface{}) CollectionInterface {
inputCol := input.(CollectionInterface)
inputCol.Process()
return inputCol
}
func makeInterface(input interface{}) interface{} {
return input
}
func main() {
test := Collection{Id: "ya"}
test.Process()
testInt := makeInterface(test)
test0 := testInt.(CollectionInterface)
test1 := testfunc(test0)
fmt.Println(test1)
}
我只是想知道如何在不将“Process”函数更改为静态函数的情况下将interface {}转换为CollectionInterface?
答案 0 :(得分:2)
将此行test := Collection{Id: "ya"}
更改为此test := &Collection{Id: "ya"}
。该接口是为*Collection
类型实现的;不是Collection
。
答案 1 :(得分:-1)
你做错了。我做了一些修改来解决它。如果您有疑问,请告诉我。