Created
March 14, 2012 01:21
-
-
Save pjz/2033189 to your computer and use it in GitHub Desktop.
golang docstring access
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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