If I have a string, e.g. "api.Client"
, and I want the actual golang type from that, which I know exists, how can I do this?
I have scanned the reflect
package and couldn't find anything, reflect.TypeOf("api.Client")
will of course return string
.
What I really want is later then scan that type for all the methods it has, which I know I can do with reflect
. But how do I get the actual type first?
Alternatively I can provide the full path to the file where that type exists, e.g. "app/api/client.go", but I would not know how that can help.
EDIT: Question closed, but just for reference, where I was at the time of asking.
func (g *Generator) CreateClient(pkg string) {
cl := pkg[4:] + ".Client"
fmt.Println("Analyzing " + cl)
t := reflect.TypeOf(cl)
fmt.Println(t) // I get string here of course and not the actual type
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
tt := reflect.TypeOf(method)
numIn := tt.NumIn()
for i := 0; i < numIn; i++ {
inV := tt.In(i)
in_Kind := inV.Kind() //func
fmt.Printf("\nParameter IN: "+strconv.Itoa(i)+"\nKind: %v\nName: %v\n-----------", in_Kind, inV.Name())
}
}
}
pkg
is the string mentioned above, which I would get by doing a simple grep over the existing repo.