Skip to content

Instantly share code, notes, and snippets.

@wperron
Created January 28, 2022 01:39
Show Gist options
  • Save wperron/09b4ef956e654ad368980a279dc40786 to your computer and use it in GitHub Desktop.
Save wperron/09b4ef956e654ad368980a279dc40786 to your computer and use it in GitHub Desktop.
Find all functions and methods in a Go source file
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
)
func main() {
args := os.Args[1:]
file, err := parser.ParseFile(&token.FileSet{}, args[0], nil, parser.AllErrors)
if err != nil {
log.Fatal(fmt.Errorf("couldn't parse source file at %s: %s", args[0], err))
}
for _, decl := range file.Decls {
switch fn := decl.(type) {
case *ast.FuncDecl:
fmt.Println(fmt.Sprintf("func %s receiver for %s", fn.Name, "nil"))
default:
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment