readonly is a Go linter that enforces the immutability of struct fields, preventing modifications from outside their package.
This tool offers a simpler alternative to getter methods.
go install github.com/devinalvaro/readonly/cmd/readonly@latest
readonly [package]
Consider the following struct:
package example
type Config struct {
Name string `readonly:"enforce"`
Value string
}readonly will report an error if Config.Name is modified from outside the example package.
Another example:
package example
type Config struct {
Name string `readonly:"enforce_all"`
Value string
}readonly now enforces both Config.Name and Config.Value.
The test file shows all cases that readonly covers.