The readonly
package provides a simple and efficient way to handle immutable and final values in Go. It offers two main types: ReadOnly
and Final
, each designed to manage values that are either immutable or assigned once.
To use the readonly
package in your Go project, simply import it as follows:
import "github.com/STRockefeller/readonly"
The ReadOnly
struct is used to create immutable values. Once a value is set, it cannot be changed.
value := readonly.NewReadOnly(42)
v := value.Get()
The Final
struct allows for creating a value that can be set only once. It's an alternative to ReadOnly
for scenarios where the value might not be known at the time of instantiation.
var finalValue readonly.Final[int]
To set the value for the first time:
finalValue.Set(42)
To safely access the value:
v := finalValue.Get()
- Immutable value handling with
ReadOnly
. - Single-assignment value handling with
Final
. - Type-safe implementation using Go generics.