我想定义一个函数来运行一个回调函数,回调函数会是不确定的,比如args或者returns都是不确定的。
我试试这个:
package main
import (
"fmt"
)
func F(callback func(args ...interface{}) interface{}, args ...interface{}) {
rev := callback(args...)
fmt.Println(rev)
}
func CB1() {
fmt.Println("CB1 called")
}
func CB2() bool {
fmt.Println("CB2 called")
return false
}
func CB3(s string) {
fmt.Println("CB3 called")
}
func CB4(s string) bool {
fmt.Println("CB4 called")
return false
}
func main() {
F(CB1)
F(CB2)
F(CB3, "xxx")
F(CB4, "yyy")
}
Run Code Online (Sandbox Code Playgroud)
错误:
./play.go:31:3: cannot use CB1 (type func()) as type func(...interface {}) interface {} in argument to F …Run Code Online (Sandbox Code Playgroud) 我试过了
koan-engine.runner=> (map identity [1 2 3])
(1 2 3)
koan-engine.runner=> (type (map identity [1 2 3]))
clojure.lang.LazySeq
koan-engine.runner=> (type '(1 2 3))
clojure.lang.PersistentList
Run Code Online (Sandbox Code Playgroud)
但它看起来像一个列表,实际上是一个LazySeq,那么如何将向量转换为列表?