https://github.com/stokito/go-idnum
I simple struct with pair of integer and it's string representation.
https://github.com/stokito/go-idnum
golang zero-allocation
Last synced: about 1 year ago
JSON representation
I simple struct with pair of integer and it's string representation.
- Host: GitHub
- URL: https://github.com/stokito/go-idnum
- Owner: stokito
- License: 0bsd
- Created: 2023-05-28T08:23:19.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-08T05:54:54.000Z (over 2 years ago)
- Last Synced: 2025-04-02T13:07:11.096Z (about 1 year ago)
- Topics: golang, zero-allocation
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# go-idnum Numeric ID struct
If you have some id or integer used on a program then you'll waste some memory and CPU for conversions to string and from string.
Instead you can create the IdNum struct that is just a pair of integer and it's string.
Then you can call multiple times it's String() method without any new allocations.
The type supports JSON serialization and deserialization
## Usage
```go
idNum1 := NewIdNum(42)
idNum2 := NewIdNumFromStr("42")
idNum3 := NewIdNumFromBytes([]byte("42"))
idNum1.Num == 42
idNum1.Str == "42"
idNum1.String() == "42"
// You can use as a field type in DTO
type User struct {
Id IdNum
}
body, err := json.Marshal(u)
body == `{"Id":42}`
```
## Install
go get -u github.com/stokito/go-idnum
## License
[0BSD](https://opensource.org/licenses/0BSD) (similar to Public Domain)