At the moment, I'm stuck in this code : 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)
}
I'm just wondering how can I convert the interface{} into CollectionInterface without changing the "Process" function into a static function?
You are doing it wrong. I make little modification to fix it. Let me know if you have question.
Change this line test := Collection{Id: "ya"}
to this test := &Collection{Id: "ya"}
. The interface is implemented for type *Collection
; not Collection
.