must

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: MIT Imports: 2 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
package main

import (
	"fmt"

	"github.com/xoctopus/x/misc/must"
)

func ReturnTrue() bool {
	return true
}

func ReturnFalse() bool {
	return false
}

func main() {
	must.BeTrue(ReturnTrue())

	defer func() {
		fmt.Println(recover())
	}()
	must.BeTrue(ReturnFalse())

}
Output:
must be true

func BeTrueV

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

import (
	"fmt"

	"github.com/xoctopus/x/misc/must"
)

func ReturnIntTrue() (int, bool) {
	return 100, true
}

func ReturnIntFalse() (int, bool) {
	return 100, false
}

func main() {
	fmt.Println(must.BeTrueV(ReturnIntTrue()))

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

}
Output:
100
must be true

func BeTrueWrap

func BeTrueWrap(ok bool, msg string, args ...any)
Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/misc/must"
)

func ReturnTrue() bool {
	return true
}

func ReturnFalse() bool {
	return false
}

func main() {
	must.BeTrueWrap(ReturnTrue(), "any")

	defer func() {
		fmt.Println(recover())
	}()
	must.BeTrueWrap(ReturnFalse(), "required exists")

}
Output:
must be true: required exists

func NoError

func NoError(err error)
Example
package main

import (
	"fmt"

	"github.com/pkg/errors"

	"github.com/xoctopus/x/misc/must"
)

func ReturnError() error {
	return errors.New("some error")
}

func ReturnNoError() error {
	return nil
}

func main() {
	must.NoError(ReturnNoError())

	defer func() {
		fmt.Println(recover())
	}()
	must.NoError(ReturnError())

}
Output:
some error

func NoErrorV

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

import (
	"fmt"

	"github.com/pkg/errors"

	"github.com/xoctopus/x/misc/must"
)

func ReturnIntError() (int, error) {
	return 100, errors.New("some error")
}

func ReturnIntNoError() (int, error) {
	return 100, nil
}

func main() {
	fmt.Println(must.NoErrorV(ReturnIntNoError()))

	defer func() {
		fmt.Println(recover())
	}()
	must.NoErrorV(ReturnIntError())

}
Output:
100
some error

func NoErrorWrap

func NoErrorWrap(err error, msg string, args ...any)
Example
package main

import (
	"fmt"

	"github.com/pkg/errors"

	"github.com/xoctopus/x/misc/must"
)

func ReturnError() error {
	return errors.New("some error")
}

func ReturnNoError() error {
	return nil
}

func main() {
	must.NoErrorWrap(ReturnNoError(), "any")

	defer func() {
		fmt.Println(recover())
	}()
	must.NoErrorWrap(ReturnError(), "some message: %d", 10)

}
Output:
some message: 10: some error

func NotNilV added in v0.0.14

func NotNilV[V any](v V) V
Example
package main

import (
	"fmt"
	"reflect"

	"github.com/xoctopus/x/misc/must"
	"github.com/xoctopus/x/ptrx"
)

func main() {
	func() {
		defer func() {
			fmt.Println(recover())
		}()
		must.NotNilV((*int)(nil))
	}()

	func() {
		defer func() {
			fmt.Println(recover())
		}()
		must.NotNilV(any((*int)(nil)))
	}()

	func() {
		defer func() {
			fmt.Println(recover())
		}()
		fmt.Println(must.NotNilV(1))
		fmt.Println(*must.NotNilV(ptrx.Ptr(1)))
		must.NotNilV(reflect.TypeOf(nil))
	}()

}
Output:
must not nil: invalid value
must not nil: invalid value
1
1
must not nil: invalid value

func NotNilWrap added in v0.0.15

func NotNilWrap(v any, msg string, args ...any)
Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/misc/must"
)

func main() {
	func() {
		defer func() {
			fmt.Println(recover())
		}()
		must.NotNilWrap((*int)(nil), "invalid business data1")
	}()

	func() {
		defer func() {
			fmt.Println(recover())
		}()
		must.NotNilWrap(any((*int)(nil)), "invalid business data2")
	}()

}
Output:
must not nil, but got invalid value invalid business data1
must not nil, but got invalid value invalid business data2

Types

This section is empty.

Jump to

Keyboard shortcuts

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