0

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.

transient_loop
  • 5,144
  • 12
  • 46
  • 95
  • 2
    Go is a statically typed, and compiled language. You cannot get types that were not present during compilation. – JimB Jan 07 '21 at 17:03
  • 1
    The path to a source file is meaningless at runtime because the source is not used at runtime. – Adrian Jan 07 '21 at 17:07
  • They are present during compilation. Not asking for a non-existing type, it's clearly stated in the question. I want to scan an existing repo for `Client` instances and generate a go client binary which can be used to query from the command line. – transient_loop Jan 07 '21 at 17:13
  • Just because the files exist at compile-time doesn't mean they are compiled. If they were compiled into your binary, you would have the type available. You're asking about `reflect`, which is executed at runtime. If you want to analyze and generate code to be compiled later, show an example of what you're trying to do. – JimB Jan 07 '21 at 17:18
  • So I wanted to scan an existing repo (not in my control) - let's call it BinaryA. This repo has multiple packages which have a `Client` type with functions. Of course, those `Client` funcs can only be used from inside that repo at runtime. I wanted to scan that repo for those `Client`, giving me an array of strings (the types), then analyze those via reflect and generate BinaryB which would allow to execute those Client functions from the command line and query BinaryA, which is a kind of a server. I know it all sounds weird but couldn't come up with any better way to do it. Never mind. – transient_loop Jan 07 '21 at 17:28
  • 1
    You are still confusing runtime ad compilation, you cannot use `reflect` to analyze things you have not compiled. It sounds like want some of the functionality provided by the [`go/*` packages](https://pkg.go.dev/go/) – JimB Jan 07 '21 at 18:27
  • @transient_loop I second JimB's comments, what you describe sounds more like source-code-analysis and not runtime-type-analysis... maybe this is what you're looking for? https://play.golang.org/p/lqKtJF0MVWX (note: the code doesn't work on playground, you need to run it on a machine on which `go` is installed and available to the program because `"golang.org/x/tools/go/packages"` depends on it) – mkopriva Jan 07 '21 at 19:33

0 Answers0