代码如下:
```go
package main
import (
"fmt"
)
type Task struct {
f func(i interface{})
}
func NewTask(h func(interface{})) (t *Task) {
t = &Task{
f: h,
}
return
}
func (t *Task) Run() {
fmt.Println("Runing...")
}
func test1(i *int) error {
fmt.Println("test1", i)
// do something here
// ...
return nil
}
func test2(s *string) (err error) {
fmt.Println("test2", s)
// do otherthing here
// ...
return
}
func main() {
t1 := NewTask(test1)
t2 := NewTask(test2)
t1.Run()
t2.Run()
}
```
报错:
.\main.go:33:15: cannot use test1 (type func(*int) error) as type func(interface {}) in argument to NewTask
.\main.go:34:15: cannot use test2 (type func(*string) error) as type func(interface {}) in argument to NewTask
请教诸位如何修改?谢谢
伪代码
```go
type task interface{
Run()
}
type t1 struct{}
func(t *t1) Run(){}
func NewTask() task{}
func main(){
t := NewTask()
t.Run()
}
```
#6
更多评论
```go
package main
import "fmt"
type task struct {
f func(i interface{})
}
func (t *task) run() *task {
return &task{
f: func(i interface{}) {
fmt.Println(i)
},
}
}
func allType() *task {
return &task{
f: func(i interface{}) {
fmt.Println(i)
},
}
}
func main() {
a := allType()
a.f("aaa")
var b task
b1 := b.run()
b1.f("bbb")
}
```
是不是就是想简单实现这个??
#2