syntaxgo is a Go toolkit built on go/ast Abstract Syntax Tree and reflect package.
syntaxgo is designed to ease code analysis and automated generation.
π― AST Parsing: Parse Go source code into Abstract Syntax Tree with multiple parsing modes β‘ Type Reflection: Extract package paths, type info, and generate usage code π Code Search: Find functions, types, and struct fields in AST π Tag Operations: Extract and manipulate struct field tags π οΈ Code Generation: Generate function params, return values, and variable definitions π¦ Import Management: Auto inject missing imports and manage package paths
go get github.com/yyle88/syntaxgopackage main
import (
"fmt"
"github.com/yyle88/must"
"github.com/yyle88/runpath"
"github.com/yyle88/syntaxgo/syntaxgo_ast"
)
func main() {
// Parse Go source file from current path
astBundle, err := syntaxgo_ast.NewAstBundleV4(runpath.Current())
if err != nil {
panic(err)
}
// Extract package name from AST
pkgName := astBundle.GetPackageName()
fmt.Println("Package name:", pkgName)
// Print complete AST structure to console
must.Done(astBundle.Print())
}β¬οΈ Source: Source
package main
import (
"fmt"
"reflect"
"github.com/yyle88/syntaxgo/syntaxgo_reflect"
)
type MyStruct struct {
Name string // User name field
Age int // User age field
}
func main() {
// Extract package path from type instance
pkgPath := syntaxgo_reflect.GetPkgPath(MyStruct{})
fmt.Println("Package path:", pkgPath)
// Generate qualified type usage code
typeCode := syntaxgo_reflect.GenerateTypeUsageCode(reflect.TypeOf(MyStruct{}))
fmt.Println("Type usage code:", typeCode)
}β¬οΈ Source: Source
package main
import (
"fmt"
"go/parser"
"go/token"
"github.com/yyle88/must"
"github.com/yyle88/syntaxgo/syntaxgo_search"
)
func main() {
// Parse Go source code from string
fset := token.NewFileSet()
src := `package example
func HelloWorld() string {
return "Hello, World!"
}
type Person struct {
Name string
Age int
}
`
astFile, err := parser.ParseFile(fset, "", src, parser.ParseComments)
if err != nil {
panic(err)
}
// Search function declaration in AST
astFunc := syntaxgo_search.FindFunctionByName(astFile, "HelloWorld")
if astFunc != nil {
fmt.Println("Found function:", astFunc.Name.Name)
}
// Search struct type definition in AST
structType, ok := syntaxgo_search.FindStructTypeByName(astFile, "Person")
must.OK(ok)
// Traverse struct fields and print field names
for _, field := range structType.Fields.List {
for _, fieldName := range field.Names {
fmt.Println("Person Field:", fieldName.Name)
}
}
}β¬οΈ Source: Source
package main
import (
"fmt"
"github.com/yyle88/syntaxgo/syntaxgo_tag"
)
func main() {
// Define struct field tag with GORM and JSON annotations
tag := `gorm:"column:id;type:bigint" json:"id"`
// Extract GORM tag value from complete tag string
gormTag := syntaxgo_tag.ExtractTagValue(tag, "gorm")
fmt.Println("GORM tag:", gormTag)
// Extract column field value from GORM tag
column := syntaxgo_tag.ExtractTagField(gormTag, "column", syntaxgo_tag.EXCLUDE_WHITESPACE_PREFIX)
fmt.Println("Column name:", column)
// Extract type field value from GORM tag
typeValue := syntaxgo_tag.ExtractTagField(gormTag, "type", syntaxgo_tag.EXCLUDE_WHITESPACE_PREFIX)
fmt.Println("Column type:", typeValue)
}β¬οΈ Source: Source
Core Functions:
NewAstBundleV1/V2/V3/V4/V5/V6- Parse Go source files with different input modes (bytes, path, FileSet)FormatSource- Format AST back to formatted Go source codeSerializeAst- Serialize AST into text representationGetPackageName- Extract package name from ASTAddImport/DeleteImport- Manage single import pathsAddNamedImport/DeleteNamedImport- Handle aliased imports (like_ "embed")InjectImports- Auto inject missing imports into source codeCreateImports- Generate import block from package paths
Use Cases:
- Parse source files with comment preservation
- Auto fix missing imports in generated code
- Extract package info when scanning projects
- Format code after AST manipulation
Core Functions:
NewNode/NewNodeV1/NewNodeV2- Create nodes with position infoGetCode/GetText- Extract code content from AST nodesSdxEdx- Get start/end indices of nodesDeleteNodeCode- Remove node code from sourceChangeNodeCode- Replace node content with new codeChangeNodeCodeSetSomeNewLines- Replace with added newlines
Use Cases:
- Extract function bodies from AST
- Replace struct field definitions
- Delete unused code sections
- Insert new code at specific positions
Core Functions:
NewNameTypeElements- Extract params/returns from AST field listExtractNameTypeElements- Process field lists with custom namingNames/Kinds- Get param names and types as separate listsFormatNamesWithKinds- Format as "name type" pairsGenerateFunctionParams- Create params when calling functions (handle variadic)GenerateVarDefinitions- Create "var name type" statementsGroupVarsByKindToLines- Group vars with same typeFormatAddressableNames- Add "&" prefix to namesSimpleMakeNameFunction- Auto name generator (arg0, arg1, res0, res1)
Use Cases:
- Generate wrapping functions with same signature
- Create mock implementations from interfaces
- Extract function signatures to document APIs
- Generate test setup code with typed variables
Core Functions:
GetPkgPath/GetPkgPathV2- Get package path from type/generic typeGetPkgName/GetPkgNameV2- Extract package name from typeGetPkgPaths- Batch get paths from multiple typesGetTypes- Get reflect types from objectsGenerateTypeUsageCode- Generate qualified type name (pkg.Type)GetQuotedPackageImportPaths- Create quoted import paths
Use Cases:
- Auto generate import statements based on used types
- Create qualified type names in code generation
- Discover package dependencies from types
- Generate type-safe code with proper imports
Core Functions:
FindFunctionByName- Locate function declaration in ASTFindStructTypeByName- Find struct definitionFindInterfaceTypeByName- Find interface definitionFindClassesAndFunctions- Extract top-most declarationsGetStructFields- Get all fields from structGetStructFieldNames- Extract field namesGetInterfaceMethods- List interface methodsGetArrayElementType- Get element type of arrays/slices
Use Cases:
- Find functions when analyzing code
- Extract struct definitions in ORM mapping
- List interface methods to check implementations
- Navigate complex AST structures
Core Functions:
ExtractTagValue- Get complete tag content (e.g.,gorm:"column:id;type:bigint")ExtractTagField- Extract field value (e.g.,columnβid)ExtractTagValueIndex/ExtractTagFieldIndex- Get values with position infoSetTagFieldValue- Update/insert tag fieldsExtractNoValueFieldNameIndex- Find flags likeprimaryKeyExtractFieldEqualsValueIndex- Find fields with specific values
Use Cases:
- Parse GORM/JSON tags in struct definitions
- Generate database column mappings
- Update struct tags in code generation
- Validate tag formats in linters
MIT License. See LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Found a mistake? Open an issue on GitHub with reproduction steps
- π‘ Have a feature idea? Create an issue to discuss the suggestion
- π Documentation confusing? Report it so we can improve
- π Need new features? Share the use cases to help us understand requirements
- β‘ Performance issue? Help us optimize through reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo to get new releases and features
- π Success stories? Share how this package improved the workflow
- π¬ Feedback? We welcome suggestions and comments
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git). - Navigate: Navigate to the cloned project (
cd repo-name) - Branch: Create a feature branch (
git checkout -b feature/xxx). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes and use significant commit messages
- Stage: Stage changes (
git add .) - Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! πππ