gomacro icon indicating copy to clipboard operation
gomacro copied to clipboard

How to get help of a method?

Open dlintw opened this issue 6 years ago • 5 comments

for example, show call shell of "go doc fmt.Println" to show document


import "fmt"
fmt.Println?

dlintw avatar Jul 23 '19 07:07 dlintw

There is no mechanism at the moment to retrieve and print the documentation of a function, type, method, variable or constant.

It would definitely be useful. Do you know about an API (except launching another process, as go doc is) to retrieve such documentation?

Currently, evaluating fmt.Println will show the function value and its signature (i.e. its type), but not its documentation:

gomacro> import "fmt"
gomacro> fmt.Println
0x1993260       // func(...interface{}) (int, error)

cosmos72 avatar Jul 24 '19 12:07 cosmos72

Stackoverflow has an answer, and it's not really encouraging: https://stackoverflow.com/questions/54558527/how-to-get-func-documentation-in-golang There is no straightforward API - one needs to locate and parse the relevant source code in order to retrieve (all) its comments

cosmos72 avatar Jul 24 '19 15:07 cosmos72

Does this helpful? https://github.com/fatih/vim-go-tutorial#documentation-lookup In vim, we can use :GoDoc to get the document I guess the easier implementation is just call the shell to execute "go doc fmt.Println"

dlintw avatar Jul 24 '19 23:07 dlintw

I agree, the quickest solution is to execute go doc fmt.Println. I will probably implement this.

The alternative is to find the sources, parse them, pass them to go/doc.New(), and finally retrieve the documentation from the created doc.Package

cosmos72 avatar Jul 26 '19 09:07 cosmos72

the new gopls (https://github.com/golang/tools/tree/master/gopls) lets you jump to code definitions, quickly. And typically the docs are just above the function, so that could work nicely. It is obviously written in Go, but I'm not sure about the library API. Might be usable from Go. It does seem that most of the package surface (https://github.com/golang/tools/tree/master/gopls) is internal. But since it is supposed to be a long running server process, it might be fine to start it once on demand and then query/update.

glycerine avatar Oct 06 '19 22:10 glycerine