Colgen is a powerful Go code generation tool that automates the creation of collection methods and types based on special comment annotations.
- Generates collection types and utility methods from simple annotations
- Supports multiple generation modes:
- Base collection types (
[]Struct
) - Field value collectors
- Unique field value collectors
- Group slice by field
- Converters and Index functions
- Base collection types (
- AI-assisted review/readme/tests generation
- Code injection capabilities
- Customizable through command-line flags
go install github.com/vmkteam/colgen/cmd/colgen@latest
//go:generate colgen
//colgen:News,Category,Tag
//colgen:News:TagIDs,UniqueTagIDs,Map(db),UUID
//colgen:Episode:ShowIDs,MapP(db.SiteUser),Index(MovieID),Group(ShowID)
//colgen:Show:MapP(db)
//colgen:Season:mapp(db)
Flag | Description | Default |
---|---|---|
-list |
Use "List" suffix for collections | false |
-imports |
Custom import paths (comma-separated) | "" |
-funcpkg |
Package for Map & MapP functions | "" |
-write-key |
Write assistant key to homedir | "" |
-ai |
Choose assistant whose key is being written | "deepseek" |
For //colgen:<struct>,<struct>,...
:
- Creates
type <struct>s []<struct>
- Generates methods:
IDs() []<id type>
- Returns all IDs in sliceIndex() map[<id type>]<struct>
- Returns map of ID to struct
Index(field)
- Create index by specified field (default: ID)Group(field)
- Group slice by specified field<Field>
- Collect all values from fieldUnique<Field>
- Collect unique values from fieldMapP
- Generate mapping function with package prefixMap
- Generate mapping function (can be lowercase for private)
//colgen@NewCall(db)
//colgen@newUserSummary(newsportal.User,full,json)
colgen -write-key=<deepseek key>
It writes deepseek key to the config file. If you want to use claude, run:
colgen -write-key=<claude key> -ai=claude
Supported assistants at the moment: deepseek, claude.
Note: if you run it not the first time, it replaces only key of chosen assistant. So, you can add both keys and choose assistant to run from a special comment.
//go:generate colgen
//colgen@ai:<readme|review|tests>
Examples:
//go:generate colgen
//colgen@ai:readme // makes readme using deepseek by default
//colgen@ai:tests(deepseek) // makes tests using deepseek explicitly
//colgen@ai:review(claude) // makes review using claude