I have this code:
package main
import (
"fmt"
"reflect"
)
type cmd struct{
Echo func(string) (string,error)
}
func main() {
cmd := cmd{
Echo : func(arg string) (string, error) {
return arg, nil
},
}
result := reflect.ValueOf(cmd).FieldByName("Echo").Call([]reflect.Value{reflect.ValueOf("test")})
if result[1] == nil{
fmt.Println("ok")
}
}
我想检查我的错误是否为空,但是在我的代码中,这并不起作用,因为它有不同的类型。我试图像这样做:
reflect[1] == reflect.Value(reflect.ValueOf(nil))
所以它具有相同的类型,但reflect.Value(reflect.ValueOf(nil))
的值不是nil,而是<invalid reflect.Value>
。
reflect.Value
中的实际值,您可以使用.Interface()
方法。https://play.golang.com/p/42fVZLqYtV3。文档:https://golang.org/pkg/reflect/#Value.Interface - undefined