README
¶
positionless
A Go static analyzer that detects positional struct literal initialization and suggests converting them to named field initialization for better code maintainability.
Why?
Positional struct literals are fragile and can lead to bugs when struct fields are reordered or new fields are added. This analyzer helps you find and fix these issues automatically.
[!WARNING] Positional struct literals break when fields are reordered or new fields are added in the middle of a struct
// Bad - positional initialization
person := Person{"John", 30, "[email protected]"}
// Good - named field initialization
person := Person{
Name: "John",
Age: 30,
Email: "[email protected]",
}
Installation
Go
go install github.com/flaticols/positionless@latest
Homebrew
brew install flaticols/apps/bump
Usage
As a standalone tool
# Analyze current directory
positionless ./...
# Analyze specific package
positionless ./pkg/mypackage
# Include generated files (excluded by default)
positionless -generated ./...
# Apply suggested fixes automatically
positionless -fix ./...
With go vet
You can use this analyzer with go vet:
# Run the analyzer with go vet
go vet -vettool=$(which positionless) ./...
# Apply fixes with go vet
go vet -vettool=$(which positionless) -fix ./...
[!TIP] Add this to your CI/CD pipeline to catch positional struct literals early
Using with other tools
This tool pairs well with fieldalignment analyzer. Run positionless first to convert positional literals to named fields, then run fieldalignment to optimize struct memory layout:
# First, fix positional initialization
positionless -fix ./...
# Then, optimize field alignment
fieldalignment -fix ./...
[!NOTE] Running
positionlessbeforefieldalignmentensures that field reordering won't break your code
In your editor
Most Go editors support running custom analyzers. Configure your editor to run this analyzer for real-time feedback.
As a GitHub Action
You can use positionless in your GitHub workflows to automatically check for positional struct literals:
name: Code Analysis
on: [push, pull_request]
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Check for positional struct literals
- uses: flaticols/positionless@v1
To automatically fix issues and commit the changes:
name: Auto-fix Positional Literals
on: [push]
jobs:
fix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Fix positional struct literals
- uses: flaticols/positionless@v1
with:
fix: true
# Commit changes if any
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: 'fix: convert positional struct literals to named fields'
Action Inputs
| Input | Description | Default |
|---|---|---|
path |
Path to analyze | ./... |
fix |
Apply suggested fixes automatically | false |
include-generated |
Include generated files in analysis | false |
version |
Version of positionless to use | latest |
How it works
The analyzer:
- Scans your Go code for struct literal initialization
- Identifies positional initialization patterns
- Suggests fixes that convert to named field initialization
- Can automatically apply fixes with the
-fixflag - Preserves your original values and formatting
- Only processes exported fields (respects Go's visibility rules)
- Skips generated files by default (use
-generatedto include them)
Example
Given this code:
type Config struct {
Host string
Port int
Timeout time.Duration
RetryMax int
}
cfg := Config{"localhost", 8080, 5 * time.Second, 3}
The analyzer will suggest:
cfg := Config{
Host: "localhost",
Port: 8080,
Timeout: 5 * time.Second,
RetryMax: 3,
}
[!IMPORTANT] The analyzer only processes exported fields to respect Go's visibility rules
License
MIT License - see LICENSE file for details
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Documentation
¶
There is no documentation for this package.