src/test_interface.git/main.go
packagemain import( task"test_interface.git/task" ) funcmain(){ do:=task.NewInter(task.NewTask()) do.OnInit() }
src/test_interface.git/task/print.go
packagetask import( "fmt" ) typeStvestruct{ } funcNewTask()Inter{ return&Stve{} } func(self*Stve)OnInit(){ fmt.Println("Hellowrold") }
src/test_interface.git/task/interface.go
packagetask import() typeInterinterface{ OnInit() } typeAppstruct{ appInter } funcNewInter(tmpInter)*App{ app1:=new(App) app1.app=tmp returnapp1 } func(self*App)OnInit(){ self.app.OnInit() }
编译
go run main.go
执行结果:
Hellowrold原文链接:https://www.f2er.com/go/190163.html