Package astp
provides AST predicates.
Go version 1.16+
go get github.com/go-toolsmith/astp
package main
import (
"fmt"
"github.com/go-toolsmith/astp"
"github.com/go-toolsmith/strparse"
)
func main() {
if astp.IsIdent(strparse.Expr(`x`)) {
fmt.Println("ident")
}
if astp.IsBlockStmt(strparse.Stmt(`{f()}`)) {
fmt.Println("block stmt")
}
if astp.IsGenDecl(strparse.Decl(`var x int = 10`)) {
fmt.Println("gen decl")
}
}