-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45dd650
commit e55c66a
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# OnceCell | ||
|
||
A thread-safe value that can only be set once | ||
|
||
--- | ||
|
||
Have you ever wanted to use a constant in your program, but did not have access to its value until runtime? | ||
|
||
Have you ever wanted to use a global variable to store command line arguments, but wanted to ensure the variable is never changed? | ||
|
||
Have you every wanted to enforce immutability with data shared by multiple threads? | ||
|
||
Well, this is the library for you! | ||
|
||
|
||
## Installation | ||
|
||
```bash | ||
go get github.com/Xavientois/once-cell/primitives | ||
``` | ||
|
||
## Usage | ||
|
||
```go | ||
import oncecell "github.com/Xavientois/once-cell/primitives" | ||
|
||
var myString primitives.OnceString | ||
|
||
// Setters | ||
myString.SetValue("hello") // Set the value once | ||
err := myString.SetValue("world") // Set the value again and an error will be returned | ||
myString.MustSetValue("world") // Will panic if the value is already set | ||
|
||
// Getters | ||
s, err := myString.Value() // Returns an error if the value has not been set | ||
s := myString.MustValue() // Panics if the value has not been set | ||
|
||
``` | ||
|
||
## Custom Types | ||
|
||
The library is currently only implemented for primitive types, at least until generics are introduced into the language. | ||
If you wish to use custom types in the meantime, you can use the `primitives/once-cell-gen.sh` script to generate an implementation for your custom type. | ||
|
||
```bash | ||
./primitives/once-cell-gen.sh YourTypeName | ||
``` | ||
|
||
The second command-line argument is the value to be used in the generated tests | ||
|
||
## License | ||
|
||
Apache 2.0 | ||
|
||
|
||
*Inspired by [once_cell](https://doc.rust-lang.org/std/lazy/struct.OnceCell.html) from Rust* |