-
Notifications
You must be signed in to change notification settings - Fork 1
/
must.go
37 lines (37 loc) · 968 Bytes
/
must.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Package must provides "design-by-contract" support.
//
// With "go build -tags debug" it enables bug checking statement.
// If a bug is found it panics.
//
// Without "-tags debug", by default "go build" ignores bug checking statement
// by emitting an "NOP" instruction.
//
// Example: a function that right-shift an integer expect the input "a" is 8*n:
//
// import (
// "math/bits"
// "github.com/openacid/must"
// )
//
// func rshift(a, b int) int {
//
// must.Be.OK(func() {
// must.Be.NotZero(b)
// must.Be.True(bits.TrailingZeros(uint(a)) > 2)
// })
//
// return a >> uint(b)
// }
// func main() {
// fmt.Println(rshift(0xf, 1))
// }
//
// With the above code:
//
// `go run` just silently ignores the expectation and print the result.
//
// `go run -tags debug` would panic because `a` does not satisfy the input
// expectation.
//
// Since 0.1.0
package must