A Go static analysis tool that enforces mandatory struct field initialization using struct tags.
go install github.com/flaticols/structfieldsenforcer@latestMark struct fields as mandatory using tags:
type Config struct {
DatabaseURL string `mandatory:""`
APIKey string `mandatory:""`
Port int `mandatory:""`
Debug bool // Optional field
}The tool detects violations like:
config := Config{
DatabaseURL: "postgres://localhost/mydb",
// Missing APIKey and Port
}# Check for violations
structfieldsenforcer ./...
# Fix violations by adding TODO comments
structfieldsenforcer -fix ./...
# Generate missing constructors
structfieldsenforcer -fix -constructors=gen ./...
# Update existing constructors
structfieldsenforcer -fix -constructors=update ./...Before fixing:
config := Config{
DatabaseURL: "postgres://localhost/mydb",
Debug: true,
}After structfieldsenforcer -fix:
config := Config{
DatabaseURL: "postgres://localhost/mydb",
Debug: true,
APIKey: // TODO: provide value for APIKey,
Port: // TODO: provide value for Port,
}After structfieldsenforcer -fix -constructors=gen:
Important
The analyzer intentionally creates compilation errors with TODO comments to ensure you don't miss mandatory fields that need values.
config := Config{
DatabaseURL: "postgres://localhost/mydb",
Debug: true,
APIKey: // TODO: provide value for APIKey,
Port: // TODO: provide value for Port,
}
func NewConfig(apikey string, databaseURL string, port int) *Config {
return &Config{APIKey: apikey, DatabaseURL: databaseURL, Port: port}
}Constructor Update Example:
If you have an existing constructor missing mandatory fields:
func NewConfig(databaseURL string) *Config {
return &Config{DatabaseURL: databaseURL}
}After structfieldsenforcer -fix -constructors=update:
func NewConfig(apikey string, databaseURL string, port int) *Config {
return &Config{APIKey: apikey, DatabaseURL: databaseURL, Port: port}
}structfieldsenforcer ./...- Check for violations in all packagesstructfieldsenforcer -fix ./...- Fix violations by adding TODO commentsstructfieldsenforcer main.go- Check specific file
structfieldsenforcer -constructors=gen ./...- Check what constructors can be generatedstructfieldsenforcer -constructors=update ./...- Check what constructors can be updatedstructfieldsenforcer -constructors=gen,update ./...- Check both operations
structfieldsenforcer -fix -constructors=gen ./...- Fix violations AND generate constructorsstructfieldsenforcer -fix -constructors=update ./...- Fix violations AND update existing constructorsstructfieldsenforcer -fix -constructors=gen,update ./...- Fix violations, generate AND update constructors
structfieldsenforcer -tag=required ./...- Use custom tag namestructfieldsenforcer -constructor-patterns="New*,Build*,Make*" ./...- Custom constructor patternsstructfieldsenforcer -fix -tag=required -constructors=gen -constructor-patterns="Build*" ./pkg/...- Complex combination
-
-tag string- Custom tag name for mandatory fields (default:"mandatory")
Example:-tag=requiredto userequired:""instead ofmandatory:"" -
-fix- Apply fixes to files. Without this flag, only reports violations without modifying files
Example:-fixenables file modifications -
-help- Show help message and exit
Example:-helpdisplays usage information
-
-constructors string- Constructor operations to perform:gen- Generate missing constructors for structs with violationsupdate- Update existing constructors to include all mandatory fieldsgen,update- Both generate and update constructors
Important: Constructor operations only modify files when combined with
-fix. Without-fix, they only report what would be done. -
-constructor-patterns string- Comma-separated patterns to identify constructor functions (default:"New*,Create*")- Supports glob patterns with
*wildcard - Examples:
"New*","Create*","Build*","New*,Create*,Build*" - Functions matching these patterns will be treated as constructors
- Supports glob patterns with
The tool accepts Go package patterns as arguments:
./...- Recursively analyze all packages in current directory (default)./pkg/...- Recursively analyze packages inpkgdirectoryfile.go- Analyze specific filepkg1 pkg2- Analyze multiple specific packages.- Analyze current directory only (non-recursive)
0- Success, no violations found1- Violations found (or would be found in check-only mode)2- Syntax or parsing errors in Go files3- File system errors (permissions, file not found, etc.)
| Flags | Behavior |
|---|---|
| (none) | Check for violations, exit with code 1 if found |
-fix |
Fix violations by adding TODO comments |
-constructors=gen |
Check what constructors can be generated |
-constructors=update |
Check what constructors can be updated |
-fix -constructors=gen |
Fix violations AND generate missing constructors |
-fix -constructors=update |
Fix violations AND update existing constructors |
-fix -constructors=gen,update |
Fix violations, generate AND update constructors |
Key Rule: Constructor operations (-constructors) only modify files when -fix is also specified.