I want to call a field of my structure with a string, so I checked out and I saw that I need to use the reflection package. Here's my code:
package main
import (
"fmt"
"reflect"
)
type cmd struct{
echo func() (string,error)
}
func main() {
cmd:=cmd{
echo : func () (string,error){
return "test", nil
},
}
fmt.Println(reflect.ValueOf(&cmd).MethodByName("echo").Call([]reflect.Value{}))
}
And when I want to build it, it panics like :
panic: reflect: call of reflect.Value.Call on zero Value
goroutine 1 [running]:
reflect.flag.mustBe(...)
c:/go/src/reflect/value.go:208
reflect.Value.Call(0x0, 0x0, 0x0, 0xc0000c7f50, 0x0, 0x0, 0x0, 0x0, 0xc000086058)
c:/go/src/reflect/value.go:319 +0x174
I do not understand the []reflect.Value{}
, why do I need to use this on the line :
reflect.ValueOf(&cmd).MethodByName("echo").Call([]reflect.Value{})
Hope someone can help ! :D
Chris
Aucun commentaire:
Enregistrer un commentaire