Categorygithub.com/Jamlie/assert
repositorypackage
0.0.0-20241004143915-2aca6f808817
Repository: https://github.com/jamlie/assert.git
Documentation: pkg.go.dev

# README

Assert

A simple asserting library in Go inspired from ThePrimeagen to help checking values in before using them to check if the logic is correct.

It can also simplify error handling.

Example

package main

import (
    "log/slog"
    "os"

    "github.com/Jamlie/assert"
)

func main() {
    val := 4

	logsFile, err := os.OpenFile("logger.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
    assert.NoError(err, "Cannot open file")
    defer logsFile.Close()

	jsonHandler := slog.NewJSONHandler(logsFile, nil)
	logger := slog.New(jsonHandler)

    logger.Info("Some message",
        "user_id", val,
    )
}

Functions

FunctionDescription
AddAssertDataAdd a key-value pair to a map to log out when the program panics
RemoveAssertDataRemove the key from the map
AssertChecks if the condition is true, if not, panics with a specified message
NoErrorChecks if the given error is nil, if not, logs out the error and panics with a specified message
EqualsChecks if two valeus are equal, if not, panics with a specified message
NotEqualsChecks if two valeus are not equal, if not, panics with a specified message
GreaterThanChecks if the first value is greater than the second one, if not, panics with a specified message
LessThanChecks if the first value is less than the second one, if not, panics with a specified message
GreaterThanEqualsChecks if the first value is greater than or equals the second one, if not, panics with a specified message
LessThanEqualsChecks if the first value is less than or equals the second one, if not, panics with a specified message
NotEmptySliceChecks if a slice is not empty, if not, panics with a specified message
NotEmptyMapChecks if a slice is not empty, if not, panics with a specified message