Go/types and go/doc

The packages go/types and go/doc are very powerful… But they seem to be completely unconnected?

In my case, I need to get detailed type information for a package (which is easy to accomplish using go/importer with go/types) but I want to have the documentation with them (which is not included in go/types, only go/doc or go/ast).

Is there any way to have types and documentation together?

An example:
How to get the go/type and the documentation for “fmt.Printf”?

Maybe I miss the point of your question, but package documentation is not included in separate documentation files but rather generated through the godoc tool from the code itself and from comments placed accordingly (e.g., above “package main” or above a function, type, or method, etc.). Is this what you are looking for?

So let’s go with an dummy example then:

type InfoIWantToGet struct {
  Type types.Type // from go/types
  Doc  string     // from go/doc
}
func getInfo(symbol string) InfoIWantToGet {
  // how? :D
}

// use: getInfo("fmt.Printf")

Like I said: I need to get the type and documentation for golang symbols.
For information on the type, I would use the package go/types with go/importer.
And for the documentation I would use go/doc with the parsed ast.
But this seems like parsing all packages twice…

package main

import (
	"fmt"
	"go/types"
	"log"
	"os/exec"
)

type InfoIWantToGet struct {
	Type types.Type // from go/types
	Doc  string     // from go/doc
}

func main() {
	info := getInfo("Println")
	fmt.Println(info)

}

func getInfo(symbol string) InfoIWantToGet {
	// how? :D
	r := InfoIWantToGet{}
	out, err := exec.Command(`godoc`, "fmt", symbol).Output()
	if err != nil {
		log.Fatal(err)
	}
	r.Doc = string(out)
	return r
}

and go/type works on a package level not a function, I quite don’t understand your intentions here…

Is parsing twice really so bad? Unless your code runs in a super time critical scenario, worrying about performance should not be your first and foremost concern. Make the code work now and optimize later (if needed at all).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.