Skip to content

Short helpers for checking invariants in your Go programs

License

Notifications You must be signed in to change notification settings

puigfp/invariant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 for more information.

Example

I'm sure that your real world programs have more interesting invariants to check than this one.

// 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

About

Short helpers for checking invariants in your Go programs

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages