dontpanic

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 License: MIT Imports: 2 Imported by: 0

README

dontpanic

Panic-free alternatives to common Go operations.

Installation

go get github.com/phelmkamp/dontpanic

Usage

func f() (err error) {
    defer dontpanic.Recover(&err)
    // do stuff that might panic...
}

Documentation

Overview

Package dontpanic provides panic-free alternatives to common operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close[T any](ch chan T) (err error)

Close invokes close(ch). Returns an error if ch is nil or closed.

func Deref

func Deref[T any](p *T) (_ T, err error)

Deref invokes *p. Returns an error if p is nil.

func Div

func Div[N constraints.Integer | constraints.Float | constraints.Complex](x, y N) (_ N, err error)

Div returns x / y. Returns an error if y is zero.

func MakeSlice

func MakeSlice[E any, S ~[]E](size ...int) (_ S, err error)

Make invokes make(S, size...). Returns an error if length is negative or larger than capacity.

Supports empty:

MakeSlice()

or length n:

MakeSlice(n)

or length n and capacity m:

MakeSlice(n, m)

func Mod

func Mod[N constraints.Integer](x, y N) (_ N, err error)

Mod returns x % y. Returns an error if y is zero.

func Recover

func Recover(err *error)

Recover calls recover() and writes the result to err.

func Send

func Send[T any](ch chan T, v T) (err error)

Send invokes ch <- v. Returns an error if ch is closed.

func SetMapIndex

func SetMapIndex[K comparable, V any, M ~map[K]V](m M, k K, v V) (err error)

SetMapIndex sets m[k] = v. Returns an error if m is nil.

func SetSliceIndex

func SetSliceIndex[E any, S ~[]E](s S, i int, v E) (err error)

SetSliceIndex sets s[i] = v. Returns an error if i is out of range.

func Slice

func Slice[E any, S ~[]E](s S, ijk ...int) (_ S, err error)

Slice returns s[i:j:k]. Returns an error if indexes are out of range or too many are specified.

Supports s[:]

Slice(s)

or s[i:]

Slice(s, i)

or s[i:j]

Slice(s, i, j)

or s[i:j:k]

Slice(s, i, j, k)

func SliceIndex

func SliceIndex[E any, S ~[]E](s S, i int) (_ E, err error)

SliceIndex returns s[i]. Returns an error if i is out of range.

func StringIndex

func StringIndex(s string, i int) (_ byte, err error)

StringIndex returns s[i]. Returns an error if i is out of range.

Types

This section is empty.

Jump to

Keyboard shortcuts

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