readonly
readonly
is a Go linter for making exported fields read-only.
Why
There are cases where we want to expose struct fields, but don't want them
modified from outside the package.
A well-known solution is to make the fields unexported, and instead export
getter functions that return them.
type ExampleStruct struct {
someField string
}
func (e ExampleStruct) SomeField() string {
return e.someField
}
With readonly
we can simply export the struct fields, because readonly
will report when they are being modified from outside the package.
// package a
type ExampleStruct struct {
SomeField string
}
// package b
var example = ExampleStruct{
SomeField: "some value",
}
example.SomeField = "new value" // Will report `readonly: field is being modified`.
You may have a look at the test files to
get an idea of all the cases that readonly
covers.
Installation
go install github.com/monadicEffect/readonly/cmd/readonly@latest
Usage
readonly [package]
readonly
doesn't support additional flags at the moment.