util

package
v1.2.18 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: MIT Imports: 5 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToStr

func BytesToStr(b []byte) string

BytesToStr converts b to string without copying. b must not be modified while the result is in use.

func BytesToValue added in v1.2.15

func BytesToValue(codec acodec.Codec, data []byte, v interface{}) error

BytesToValue decodes data into v, which should be a pointer. *[]byte gets a copy of data, *string gets string(data), *error gets errors.New(string(data)), and other types are decoded by codec, or acodec.DefaultCodec if codec is nil. It does nothing if v is nil.

func Recover

func Recover()

Recover recovers from a panic and logs it with the stack trace. It must be called directly by defer.

func Safe

func Safe(call func())

Safe calls call and recovers from any panic it raises.

func StrToBytes

func StrToBytes(s string) []byte

StrToBytes converts s to []byte without copying. The result must not be modified.

func ValueToBytes

func ValueToBytes(codec acodec.Codec, v interface{}) []byte

ValueToBytes converts v to []byte: []byte and string (or pointers to them) and error are used as-is without copying, other values are encoded by codec, or acodec.DefaultCodec if codec is nil. It returns nil if v is nil or the encoding fails.

func ValueToBytesOwned added in v1.2.18

func ValueToBytesOwned(codec acodec.Codec, v interface{}) (data []byte, owned bool)

ValueToBytesOwned is like ValueToBytes, and also reports whether the result is safe to keep after the call returns (e.g. queued for an async send) without copying.

owned is false only for []byte and *[]byte inputs, whose content the caller may still modify; callers that keep such a slice must copy it. Strings, errors and codec output cannot be modified by the caller, so owned is true.

Types

type Empty

type Empty struct{}

Empty is a zero-size type, mainly used as a signal channel element.

Jump to

Keyboard shortcuts

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