panic: reflect: call of reflect.Value.Call on zero Value

You might want to check what reflect.ValueOf returns before using it.

In this case you're getting a nil value because reflect can not see functions that aren't exported. hello starts with a lower case letter and therefore isn't exported.

Also, you're making too many pointers here, I suspect that you need to do reflect.ValueOf(c). You're doing and debugging too many things at the same time. Make a simple working example and continue debugging from there.

This works for me: https://play.golang.org/p/Yd-WDRzura


Call Kind() to check

type Header struct {
    Token string
}
    
type Request struct {
    Header 
}   


func requestedToken(request interface{}) string {
    requestValue := reflect.ValueOf(request)
    header := requestValue.FieldByName("Header12")
    if header.Kind() == 0 {
        return "" //Return here
    }
    token := header.FieldByName("Token")
    if token.Kind() == 0 {
        return ""
    }
    return token.String()
}

Just do two changes in your code. First change reflect.ValueOf(&c) to reflect.ValueOf(c) Secondly change reflect.ValueOf(command) to reflect.ValueOf(*command)

This is the code workingcode

Tags:

Reflection

Go