0

如果我有一个字符串,例如"api.Client",并且我想要其中的实际 golang 类型,我知道它存在,我该怎么做?

我已经扫描了reflect包裹,找不到任何东西,reflect.TypeOf("api.Client")当然会退货string

我真正想要的是稍后扫描该类型以查找它具有的所有方法,我知道我可以使用reflect. 但是我如何首先获得实际类型?

或者,我可以提供该类型存在的文件的完整路径,例如“app/api/client.go”,但我不知道这有什么帮助。

编辑:问题已结束,但仅供参考,我在提问时所处的位置。

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是上面提到的字符串,我可以通过对现有 repo 执行简单的 grep 来获得它。

4

0 回答 0