https://github.com/powerman/constvar
Constant values in global Go variables
https://github.com/powerman/constvar
Last synced: 4 months ago
JSON representation
Constant values in global Go variables
- Host: GitHub
- URL: https://github.com/powerman/constvar
- Owner: powerman
- License: mit
- Created: 2021-10-17T11:45:23.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-10-27T11:19:31.000Z (about 3 years ago)
- Last Synced: 2025-09-23T13:50:10.653Z (4 months ago)
- Language: Go
- Size: 15.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go package providing constant values for global variables
[](https://pkg.go.dev/github.com/powerman/constvar)
[](https://goreportcard.com/report/github.com/powerman/constvar)
[](https://github.com/powerman/constvar/releases/latest)
Some Go types can't be used for constant values, thus we've to use global
variables instead. But variables may be occasionally modified, which
usually result in hard-to-spot bugs. This package helps to work around.
## Usage
Replace this:
```go
var appStart = time.Now()
main() {
fmt.Println(appStart)
}
```
with this:
```go
var appStart = constvar.Time(time.Now())
main() {
fmt.Println(appStart())
}
```
## Safety
Of course, no one stops you from re-defining global variable, but it's
much more easier to notice (on code review) usage of constvar package
outside of defining global vars than usual variable assignment.
## Side-effects
It's now possible to set to `nil` global variables defined using constvar.
This shouldn't be a real issue because this will result in run-time panic
on accessing it value and thus won't go unnoticed.
Code is more self-documented now. :)
## Hints
### Configuration for golangci-lint
```yml
issues:
exclude-rules:
- linters: gochecknoglobals
source: "constvar[.]"
```