must

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: MIT Imports: 1 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeTrue added in v0.0.10

func BeTrue(ok bool)
Example
package main

import (
	"fmt"

	"github.com/sincospro/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 added in v0.0.10

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

import (
	"fmt"

	"github.com/sincospro/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 added in v0.0.11

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

import (
	"fmt"

	"github.com/sincospro/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/sincospro/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 added in v0.0.10

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

import (
	"fmt"

	"github.com/pkg/errors"

	"github.com/sincospro/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 added in v0.0.11

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

import (
	"fmt"

	"github.com/pkg/errors"

	"github.com/sincospro/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

Types

This section is empty.

Jump to

Keyboard shortcuts

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