must

package module
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2025 License: MIT Imports: 4 Imported by: 55

README

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

must – A Simple Assertion Library for Go

The must library simplifies assertions and panics on failure. Assert conditions directly, avoiding long checks


CHINESE README

中文说明

Features

  • Simple Error Handling: Assert conditions directly, avoiding long checks.
  • Quick Feedback: Catch bugs early with clear panic messages.
  • Lightweight & Fast: Minimal code overhead for speed. Very easy to use.
  • Versatile Assertions: Supports checking non-zero values, slice lengths.

Installation

go get github.com/yyle88/must

Quick Start

Example 1: Assert a Non-Zero Value
package main

import (
	"github.com/yyle88/must"
)

func main() {
	value := 42
	must.Nice(value) // Panics if value is zero

	println("Value is valid:", value)
}

Example 2: Validate No Error
package main

import (
	"errors"
	"github.com/yyle88/must"
)

func main() {
	err := someFunction()
	must.Done(err) // Panics if err is not nil

	println("No error encountered!")
}

func someFunction() error {
	return errors.New("unexpected error")
}

Example 3: Check Slice Length
package main

import (
	"github.com/yyle88/must"
)

func main() {
	arr := []int{1, 2, 3}
	must.Length(arr, 3) // Panics if the length is not 3

	println("Array length is correct")
}

Core Assertions

Here are the core assertions in must, summarized in a table:

Function Description Example Notes
True(v bool) Panics if v is false. must.True(isValid) Validates if v is true.
Done(err error) Panics if err is not nil. must.Done(err) Ensures no error occurred.
Must(err error) Panics if err is not nil. must.Must(err) Similar to Done.
Nice(a V) Panics if a is zero. must.Nice(value) Ensures a is non-zero.
Zero(a V) Panics if a is not zero. must.Zero(value) Ensures a is zero.
None(a V) Panics if a is non-zero. must.None(value) Ensures a is zero.
Null(v any) Panics if v is not nil. must.Null(ptr) Ensures v is nil.
Full(v any) Panics if v is nil. must.Full(value) Ensures v is non-nil.
Equals(a, b V) Panics if a and b are not equal. must.Equals(a, b) Checks if a equals b.
Same(a, b V) Panics if a and b are not equal. must.Same(a, b) Alias of Equals.
Is(a, b V) Panics if a and b are not equal. must.Is(a, b) Alias of Equals.
Ise(err, target error) Panics if err does not match target using errors.Is. must.Ise(err, targetErr) Error matching similar to errors.Is.
Ok(a V) Panics if a is zero. must.Ok(value) Ensures a is non-zero.
OK(a V) Alias of Ok, checks for non-zero value. must.OK(value) Similar to Ok.
TRUE(v bool) Panics if v is false. must.TRUE(isValid) Alias of True.
FALSE(v bool) Panics if v is true. must.FALSE(isError) Ensures v is false.
False(v bool) Panics if v is true. must.False(isError) Similar to FALSE.
Have(a []T) Panics if a is empty. must.Have(slice) Ensures a is not empty.
Length(a []T, n int) Panics if a length is not n. must.Length(slice, 3) Ensures a length is n.
Len(a []T, n int) Alias of Length, ensures a length is n. must.Len(slice, 3) Validates a length.
In(v T, a []T) Panics if v is not in a. must.In(value, slice) Ensures v is in a.
Contains(a []T, v T) Panics if a does not contain v. must.Contains(slice, value) Ensures a contains v.

License

MIT License. See LICENSE.


Contributing

Contributions are welcome! To contribute:

  1. Fork the repo on GitHub (using the webpage interface).
  2. Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate to the cloned project (cd repo-name)
  4. Create a feature branch (git checkout -b feature/xxx).
  5. Stage changes (git add .)
  6. Commit changes (git commit -m "Add feature xxx").
  7. Push to the branch (git push origin feature/xxx).
  8. Open a pull request on GitHub (on the GitHub webpage).

Please ensure tests pass and include relevant documentation updates.


Support

Welcome to contribute to this project by submitting pull requests and reporting issues.

If you find this package valuable, give me some stars on GitHub! Thank you!!!

Thank you for your support!

Happy Coding with this package! 🎉

Give me stars. Thank you!!!


GitHub Stars

starring

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func C0

func C0(err error)

func C1

func C1[T1 comparable](v1 T1, err error) T1

func C2

func C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)

func Contains

func Contains[T comparable](a []T, v T)

Contains checks if the slice contains the value. Panics if the value is not found. Contains 检查切片是否包含该值。如果未找到该值,则触发 panic。

func Diff added in v0.0.17

func Diff[V comparable](a, b V)

func Different added in v0.0.16

func Different[V comparable](a, b V)

func Done

func Done(err error)

Done expects no error. Panics if the provided error is non-nil. Done 期望没有错误。如果提供的错误不为 nil,则触发 panic。

func Equals

func Equals[V comparable](a, b V)

Equals expects the values to be equal. Panics if they are not equal. Equals 期望值相等。如果值不相等,则触发 panic。

func FALSE

func FALSE(v bool)

FALSE expects the value to be false. Panics if the value is true. FALSE 期望值为 false。如果值为 true,则触发 panic。

func False

func False(v bool)

False expects the value to be false. Panics if the value is true. False 期望值为 false。如果值为 true,则触发 panic。

func Full

func Full[T any](v *T) *T

Full expects the value to be non-nil. Panics if the value is nil. Full 期望值为非 nil。如果值为 nil,则触发 panic。

func Have

func Have[T any](a []T) []T

Have checks that the slice is not empty/none. Panics if the slice is empty/none. Have 检查切片是否为空。如果切片为空,则触发 panic。

func In

func In[T comparable](v T, a []T)

In checks if the value is in the slice. Panics if the value is not found. In 检查值是否在切片中。如果未找到该值,则触发 panic。

func Is

func Is[V comparable](a, b V)

Is expects equality, not the logic of errors.Is, but the logic of Equals. Panics if the values are not equal. Is 期望相等,不是 errors.Is 的逻辑,而是 Equals 的逻辑。如果值不相等,则触发 panic。

func Ise

func Ise(err, target error)

Ise expects the errors to be equal, similar to the behavior of errors.Is. Panics if they are not equal. Ise 期望错误相等,类似于 errors.Is 的行为。如果错误不相等,则触发 panic。

func Len

func Len[T any](a []T, n int)

Len is an abbreviation of Length, serving the same purpose. Panics if the length is not n. Len 是 Length 的缩写,功能相同。如果长度不是 n,则触发 panic。

func Length

func Length[T any](a []T, n int)

Length expects the slice to have length n. Panics if the length is not n. Length 期望切片的长度为 n。如果长度不是 n,则触发 panic。

func Must

func Must(err error)

Must expects no error. Panics if the provided error is non-nil. Must 期望没有错误。如果提供的错误不为 nil,则触发 panic。

func Nice

func Nice[V comparable](a V) V

Nice expects a non-zero value. Panics if the value is zero, returns the value if non-zero. Nice 期望一个非零值。如果值为零,则触发 panic;如果值非零,则返回该值。

func None

func None[V comparable](a V)

None expects a zero value (empty/none). Panics if the value is non-zero. None 期望值为零(空)。如果值不为零,则触发 panic。

func Null

func Null[T any](v *T)

Null expects the value to be nil. Panics if the value is non-nil. Null 期望值为 nil。如果值不为 nil,则触发 panic。

func OK

func OK[V comparable](a V)

OK expects a non-zero value. Panics if the value is zero. Provides an alternative name for preference. OK 期望一个非零值。如果值为零,则触发 panic。提供一个偏好的替代名称。

func Ok

func Ok[V comparable](a V)

Ok expects a non-zero value. Panics if the value is zero. Ok 期望一个非零值。如果值为零,则触发 panic。

func P0

func P0(err error)

func P1

func P1[T1 any](v1 *T1, err error) *T1

func P2

func P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)

func Same

func Same[V comparable](a, b V)

Same expects the values to be same. Panics if they are not same. Same 期望值相等。如果值不相等,则触发 panic。

func SameNice added in v0.0.18

func SameNice[V comparable](a, b V) V

SameNice expects the values to be the same and non-zero. Panics if they are not the same or if the value is zero. Returns the value if the conditions are met. SameNice 期望值相等且非零。如果值不相等或为零,则触发 panic。如果条件满足,则返回该值。

func Sane added in v0.0.19

func Sane[V comparable](a, b V) V

Sane means same && nice Sane 期望值相等且非零。如果值不相等或为零,则触发 panic。如果条件满足,则返回该值。

func TRUE

func TRUE(v bool)

TRUE expects the value to be true. Panics if the value is false. TRUE 期望值为 true。如果值为 false,则触发 panic。

func True

func True(v bool)

True expects the value to be true. Panics if the value is false. True 期望值为 true。如果值为 false,则触发 panic。

func V0

func V0(err error)

func V1

func V1[T1 any](v1 T1, err error) T1

func V2

func V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)

func Zero

func Zero[V comparable](a V)

Zero expects a zero value. Panics if the value is non-zero. Zero 期望值为零。如果值不为零,则触发 panic。

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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