must

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 3 Imported by: 15 modules, 40 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeTrue

func BeTrue(ok bool)
Example
must.BeTrue(true)
func() {
	defer func() {
		fmt.Println(recover())
	}()
	must.BeTrue(false)
}()

func() {
	defer func() {
		fmt.Println(recover())
	}()
	must.BeTrueWrap(false, fmt.Errorf("BeTrueWrap: some error"))
}()

func() {
	defer func() {
		fmt.Println(recover())
	}()
	must.BeTrueWrapF(false, fmt.Errorf("some error"), "BeTrueWrapF: message and args: %d", 100)
}()

f1 := func() (int, bool) { return 100, true }
f2 := func() (int, bool) { return 100, false }

fmt.Println(must.BeTrueV(f1()))
func() {
	defer func() {
		fmt.Println(recover())
	}()
	_ = must.BeTrueV(f2())
}()

must.BeTrueF(true, "any")
func() {
	defer func() {
		fmt.Println(recover())
	}()
	must.BeTrueF(false, "BeTrueF: required exists")
}()
Output:
must be true
must be true: [err: BeTrueWrap: some error]
must be true: BeTrueWrapF: message and args: 100 [err: some error]
100
must be true
must be true: BeTrueF: required exists

func BeTrueF added in v0.2.0

func BeTrueF(b bool, msg string, args ...any)

func BeTrueV

func BeTrueV[V any](v V, ok bool) V

func BeTrueWrap

func BeTrueWrap(b bool, w error)

func BeTrueWrapF added in v0.2.0

func BeTrueWrapF(b bool, w error, msg string, args ...any)

func IdenticalTypes added in v0.0.33

func IdenticalTypes(t1, t2 any) bool

func NoError

func NoError(err error)
Example
must.NoError(nil)
func() {
	defer func() {
		fmt.Println(recover())
	}()
	must.NoError(fmt.Errorf("NoError: some error"))
}()

fmt.Println(must.NoErrorV(100, nil))
func() {
	defer func() {
		fmt.Println(recover())
	}()
	must.NoErrorV(100, fmt.Errorf("NoErrorV: some error"))
}()

must.NoErrorF(nil, "any")

func() {
	defer func() {
		fmt.Println(recover())
	}()
	must.NoErrorF(fmt.Errorf("some error"), "NoErrorF: some message: %d", 10)
}()
Output:
NoError: some error
100
NoErrorV: some error
NoErrorF: some message: 10: some error

func NoErrorF added in v0.2.0

func NoErrorF(err error, msg string, args ...any)

func NoErrorV

func NoErrorV[V any](v V, err error) V

func NotNilF added in v0.2.0

func NotNilF(v any, msg string, args ...any)

func NotNilV added in v0.0.14

func NotNilV[V any](v V) V
Example
rv := reflect.ValueOf(struct {
	V0 any
	V7 error
	V8 reflect.Type
	V1 chan error
	V2 func()
	V3 *int
	V4 unsafe.Pointer
	V5 []int
	V6 map[string]int
}{})
for _, field := range rv.Fields() {
	func(v any) {
		defer func() {
			fmt.Println(recover())
		}()
		must.NotNilV(v)
	}(field.Interface())

	func(v any) {
		defer func() {
			fmt.Println(recover())
		}()
		must.NotNilF(v, "business message: %v", 100)
	}(field.Interface())

	func(v any) {
		defer func() {
			fmt.Println(recover())
		}()
		must.NotNilWrap(v, fmt.Errorf("business message: %v", 101))
	}(field.Interface())

	func(v any) {
		defer func() {
			fmt.Println(recover())
		}()
		must.NotNilWrapF(v, fmt.Errorf("business message: %v", 101), "more custom message and args: %d", 102)
	}(field.Interface())
}
fmt.Println(must.NotNilV(1))
fmt.Println(*must.NotNilV(new(int)))
Output:
must not nil, but got invalid value.
must not nil, but got invalid value. business message: 100
must not nil, but got invalid value. [err: business message: 101]
must not nil, but got invalid value. more custom message and args: 102 [err: business message: 101]
must not nil, but got invalid value.
must not nil, but got invalid value. business message: 100
must not nil, but got invalid value. [err: business message: 101]
must not nil, but got invalid value. more custom message and args: 102 [err: business message: 101]
must not nil, but got invalid value.
must not nil, but got invalid value. business message: 100
must not nil, but got invalid value. [err: business message: 101]
must not nil, but got invalid value. more custom message and args: 102 [err: business message: 101]
must not nil for type `chan error`.
must not nil for type `chan error`. business message: 100
must not nil for type `chan error`. [err: business message: 101]
must not nil for type `chan error`. more custom message and args: 102 [err: business message: 101]
must not nil for type `func()`.
must not nil for type `func()`. business message: 100
must not nil for type `func()`. [err: business message: 101]
must not nil for type `func()`. more custom message and args: 102 [err: business message: 101]
must not nil for type `*int`.
must not nil for type `*int`. business message: 100
must not nil for type `*int`. [err: business message: 101]
must not nil for type `*int`. more custom message and args: 102 [err: business message: 101]
must not nil for type `unsafe.Pointer`.
must not nil for type `unsafe.Pointer`. business message: 100
must not nil for type `unsafe.Pointer`. [err: business message: 101]
must not nil for type `unsafe.Pointer`. more custom message and args: 102 [err: business message: 101]
must not nil for type `[]int`.
must not nil for type `[]int`. business message: 100
must not nil for type `[]int`. [err: business message: 101]
must not nil for type `[]int`. more custom message and args: 102 [err: business message: 101]
must not nil for type `map[string]int`.
must not nil for type `map[string]int`. business message: 100
must not nil for type `map[string]int`. [err: business message: 101]
must not nil for type `map[string]int`. more custom message and args: 102 [err: business message: 101]
1
0

func NotNilWrap added in v0.0.15

func NotNilWrap(v any, w error)

func NotNilWrapF added in v0.2.0

func NotNilWrapF(v any, w error, msg string, args ...any)

func OK added in v0.2.0

func OK[T any](f func() (T, bool)) T

func Success added in v0.2.0

func Success[T any](f func() (T, error)) T
Example
x := must.Success(func() (int, error) { return 100, nil })
fmt.Println(x)

x = must.OK(func() (int, bool) { return 100, true })
fmt.Println(x)

func() {
	defer func() {
		fmt.Println(recover())
	}()
	_ = must.Success(func() (int, error) { return 0, fmt.Errorf("some error") })
}()
func() {
	defer func() {
		fmt.Println(recover())
	}()
	_ = must.OK(func() (int, bool) { return 0, false })
}()
Output:
100
100
some error
must be true

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL