Created
January 28, 2022 01:39
-
-
Save wperron/09b4ef956e654ad368980a279dc40786 to your computer and use it in GitHub Desktop.
Find all functions and methods in a Go source file
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
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