https://github.com/puigfp/invariant
Short helpers for checking invariants in your Go programs
https://github.com/puigfp/invariant
panic
Last synced: 3 months ago
JSON representation
Short helpers for checking invariants in your Go programs
- Host: GitHub
- URL: https://github.com/puigfp/invariant
- Owner: puigfp
- License: mit
- Created: 2019-06-15T19:50:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-14T18:18:37.000Z (over 6 years ago)
- Last Synced: 2024-06-20T10:07:53.025Z (over 1 year ago)
- Topics: panic
- Language: Go
- Size: 3.91 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# invariant
This short package provides helpers for checking invariant in your Go programs.
Using this package, you can easily write debug assertions (ignored in "release" mode) and regular assertions, that will make your program panic if they are violated.
Please read the [GoDoc](https://godoc.org/github.com/puigfp/invariant) for more information.
## Example
I'm sure that your real world programs have more interesting invariants to check than this one.
```go
// Fibonnacci returns what you think it returns
// n should be a positive integer.
func Fibonacci(n int) int64 {
invariant.Assert(n >= 0, "n should be positive")
var (
a int64 = 0
b int64 = 1
)
for n > 0 {
a, b = b, a + b
n--
}
return a
}
```
## References
This is inspired from an article by Matt Klein: [Crash early and crash often for more reliable software](https://medium.com/@mattklein123/crash-early-and-crash-often-for-more-reliable-software-597738dd21c5)