Skip to content

Instantly share code, notes, and snippets.

@pjz
Created March 14, 2012 01:21
Show Gist options
  • Save pjz/2033189 to your computer and use it in GitHub Desktop.
Save pjz/2033189 to your computer and use it in GitHub Desktop.
golang docstring access
#!/usr/bin/gorun
package main
import "os"
import "fmt"
import "strings"
import "go/doc"
import "go/token"
import "go/parser"
import "io/ioutil"
func funcDocs(filename string, funcname string) string {
fullsrc, err := ioutil.ReadFile(filename)
if err != nil { panic(err.String()) }
fullsrcs := string(fullsrc)
src := fullsrcs[strings.Index(fullsrcs, "\n"):]
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if err != nil { panic(err.String()) }
pkgdoc := doc.NewFileDoc(f)
for i := range pkgdoc.Funcs {
fdoc := pkgdoc.Funcs[i]
if fdoc.Name == funcname {
return fdoc.Doc
}
}
return ""
}
// These are the docs on the main function
func main() {
filename := os.Args[0]
fmt.Printf("main doc is: " + funcDocs(filename, "main"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment