ensure

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ensure provides assertion utilities for development. Assertions are used to catch programming errors early.

Deep-Ensure: 早暴露 + 契约化 + 零容忍 + 性能友好

Index

Constants

This section is empty.

Variables

View Source
var OnFailed = func(msg string) {
	panic(msg)
}

OnFailed is called when an assertion fails. Can be overridden for testing or custom behavior.

Functions

func NoError

func NoError(err error, format string, args ...any)

NoError asserts that err is nil. If not nil, panics with error details.

Usage:

ensure.NoError(json.Unmarshal(data, &obj), "failed to parse config")

func NoError1

func NoError1[T1 any](err error, format string, arg1 T1)

NoError1 is a single-argument specialized version of NoError.

func NoError2

func NoError2[T1, T2 any](err error, format string, arg1 T1, arg2 T2)

NoError2 is a two-argument specialized version of NoError.

func NotNil

func NotNil(obj any, format string, args ...any)

NotNil asserts that obj is not nil. If nil, panics with formatted message.

Usage:

ensure.NotNil(db, "database connection required")

func NotNil1

func NotNil1[T1 any](obj any, format string, arg1 T1)

NotNil1 is a single-argument specialized version of NotNil.

func NotNil2

func NotNil2[T1, T2 any](obj any, format string, arg1 T1, arg2 T2)

NotNil2 is a two-argument specialized version of NotNil.

func SetDebugMode

func SetDebugMode(enabled bool)

SetDebugMode enables or disables debug-only assertions.

func TODO

func TODO(format string, args ...any)

TODO marks incomplete implementation. Panics with a message about unimplemented functionality.

Usage:

func NewFeature() {
    ensure.TODO("NewFeature not implemented yet")
}

func True

func True(condition bool, format string, args ...any)

True asserts that condition is true. If false, panics with formatted message.

Usage:

ensure.True(len(items) > 0, "items must not be empty")
ensure.True(user != nil, "user %s not found", userID)

func True1

func True1[T1 any](condition bool, format string, arg1 T1)

True1 is a single-argument specialized version of True.

func True1Dbg

func True1Dbg[T1 any](condition bool, format string, arg1 T1)

True1Dbg is a single-argument specialized debug version.

func True2

func True2[T1, T2 any](condition bool, format string, arg1 T1, arg2 T2)

True2 is a two-argument specialized version of True.

func True2Dbg

func True2Dbg[T1, T2 any](condition bool, format string, arg1 T1, arg2 T2)

True2Dbg is a two-argument specialized debug version.

func True3

func True3[T1, T2, T3 any](condition bool, format string, arg1 T1, arg2 T2, arg3 T3)

True3 is a three-argument specialized version of True.

func TrueDbg

func TrueDbg(condition bool, format string, args ...any)

TrueDbg asserts that condition is true, but only in debug mode. In release mode, this is a no-op.

Usage:

ensure.TrueDbg(index >= 0 && index < len(arr), "index out of bounds: %d", index)

func Unreachable

func Unreachable(format string, args ...any)

Unreachable marks code that should never be reached. Always panics.

Usage:

switch status {
case Running:
    // ...
default:
    ensure.Unreachable("unexpected status: %d", status)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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