代码如下:
```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
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
<a href="/user/daiyudong" title="@daiyudong">@daiyudong</a> 谢谢回复
我是想把处理函数传进去,每个Task有不同的处理函数。
#3