A flexible code generation tool for Go projects that supports multiple generators and naming conventions.
- MongoDB model generation
- Customizable naming conventions
- Template-based code generation
- Cross-platform support (Linux, macOS, Windows)
- Multiple CPU architectures (amd64, arm64)
# Using Go
go install github.com/lewinz/go-gen@latest# Generate MongoDB model
go-gen model mongo --type user --dir ./internal/model# Required flags
--type string Model type name (e.g., user, product)
--dir string Output directory
# Optional flags
--template string Template directory or Git repository URL (default: git@github.com:Lewinz/go-gen.git)
--file-style string File naming style (snake|camel|pascal|kebab) (default "snake")The tool supports four naming conventions in templates:
{{.TypeSnake}}: snake_case (e.g., user_profile){{.TypeCamel}}: camelCase (e.g., userProfile){{.TypePascal}}: PascalCase (e.g., UserProfile){{.TypeKebab}}: kebab-case (e.g., user-profile)
- Generate a MongoDB model with default naming and template:
go-gen model mongo --type user --dir ./internal/model- Generate with custom file naming:
go-gen model mongo \
--type user \
--dir ./internal/model \
--file-style camel- Generate from a custom template:
go-gen model mongo \
--type user \
--dir ./internal/model \
--template ./template- Generate from a Git template repository:
go-gen model mongo \
--type user \
--dir ./internal/model \
--template https://github.com/your-org/go-templatesThe tool uses template files (.tpl) to generate code. Here's how the template files map to the generated files:
template/
└── mongo/
├── model.tpl # Generates: {type_snake}.go
│ # Example: user.go, product.go
│ # Contains: struct definition and CRUD operations
│
└── model_test.tpl # Generates: {type_snake}_test.go
# Example: user_test.go, product_test.go
# Contains: unit tests for the model
The following variables are available in templates:
{{.TypeSnake}}: Type name in snake_case (e.g., user_profile){{.TypeCamel}}: Type name in camelCase (e.g., userProfile){{.TypePascal}}: Type name in PascalCase (e.g., UserProfile){{.TypeKebab}}: Type name in kebab-case (e.g., user-profile){{.PackageName}}: Package name for the generated file
- Create your template directory:
mkdir -p templates/mongo- Create template files:
# templates/mongo/model.tpl
package {{.PackageName}}
type {{.TypePascal}} struct {
ID string `bson:"_id,omitempty"`
CreatedAt time.Time `bson:"created_at"`
UpdatedAt time.Time `bson:"updated_at"`
}- Generate code:
go-gen model mongo --type user --dir ./internal/model --template ./templatesYou can use templates from a Git repository:
go-gen model mongo \
--type user \
--dir ./internal/model \
--template https://github.com/your-org/go-templatesThe tool will:
- Clone the repository
- Cache it locally
- Use it for code generation
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.