hv

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

github.com/hauntedness/std/hv is a fork of github.com/samber/mo/option.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Err added in v0.23.1

func Err(err error) error

Err wraps an error into a Error, recording the stack trace and a default message.

func Errf added in v0.23.2

func Errf(format string, args ...any) error

Errf formats according to a format specifier and returns the string as a value that satisfies error. Errf also records the stack trace at the point it was called.

func Errorf added in v0.23.1

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

Errorf wraps an error into a Error, appending a formatted message. If the error is already a Error, it appends the message to the existing one.

func OrZero

func OrZero[T any](v *T) T

func Ptr added in v0.18.2

func Ptr[T any](v T) *T

func Zero added in v0.21.1

func Zero[T any]() T

Types

type Error added in v0.23.1

type Error struct {
	// contains filtered or unexported fields
}

func (*Error) Error added in v0.23.2

func (w *Error) Error() string

func (*Error) Format added in v0.23.2

func (w *Error) Format(s fmt.State, verb rune)

func (*Error) Message added in v0.23.2

func (w *Error) Message() string

func (*Error) Stack added in v0.23.2

func (w *Error) Stack() StackTrace

func (Error) StackTrace added in v0.23.2

func (s Error) StackTrace() StackTrace

func (*Error) Unwrap added in v0.23.2

func (w *Error) Unwrap() error

Unwrap provides compatibility for Go 1.13 error chains.

type Frame added in v0.23.1

type Frame uintptr

Frame represents a program counter inside a stack frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.

func (Frame) Format added in v0.23.1

func (f Frame) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags that alter the printing of some verbs, as follows:

%+s   function name and path of source file relative to the compile time
      GOPATH separated by \n\t (<funcname>\n\t<path>)
%+v   equivalent to %+s:%d

func (Frame) MarshalText added in v0.23.1

func (f Frame) MarshalText() ([]byte, error)

MarshalText formats a stacktrace Frame as a text string. The output is the same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.

type HvOption added in v0.20.0

type HvOption interface {
	// contains filtered or unexported methods
}

HvOption is used to distinguish Option[T] with other types. you may need this in rare corner cases. e.g. when you want to test a reflect.Value is hv.Option. It is hard to instantiate all possible hv.Option[T].

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

Option is a container for an optional value of type T. If value exists, Option is of type Some. If the value is absent, Option is of type None.

func From added in v0.18.2

func From[T any](value T, ok bool) Option[T]

From builds a Some Option when second argument is true, or None.

func FromPtr added in v0.18.2

func FromPtr[T any](value *T) Option[T]

FromPtr builds a Some Option when value is not nil, or None.

func None

func None[T any]() Option[T]

None builds an Option when value is absent.

func Some

func Some[T any](value T) Option[T]

Some builds an Option when value is present.

func (Option[T]) Get

func (o Option[T]) Get() (T, bool)

Get returns value and presence.

func (*Option[T]) GobDecode

func (o *Option[T]) GobDecode(data []byte) error

GobDecode implements the gob.GobDecoder interface.

func (Option[T]) GobEncode

func (o Option[T]) GobEncode() ([]byte, error)

GobEncode implements the gob.GobEncoder interface.

func (Option[T]) IsAbsent

func (o Option[T]) IsAbsent() bool

IsAbsent returns false when value is present.

func (Option[T]) IsPresent

func (o Option[T]) IsPresent() bool

IsPresent returns false when value is absent.

func (Option[T]) IsZero

func (o Option[T]) IsZero() bool

IsZero support json/v2.

func (Option[T]) MarshalBinary

func (o Option[T]) MarshalBinary() ([]byte, error)

MarshalBinary is the interface implemented by an object that can marshal itself into a binary form.

func (Option[T]) MarshalJSON

func (o Option[T]) MarshalJSON() ([]byte, error)

MarshalJSON encodes Option into json.

func (Option[T]) MarshalText

func (o Option[T]) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Option[T]) MustGet

func (o Option[T]) MustGet() T

MustGet panic if value is not present.

func (Option[T]) OrElse

func (o Option[T]) OrElse(fallback T) T

OrElse returns value if present or default value.

func (Option[T]) OrEmpty

func (o Option[T]) OrEmpty() T

OrEmpty returns value if present or empty value.

func (*Option[T]) Scan

func (o *Option[T]) Scan(src any) error

Scan implements the SQL sql.Scanner interface.

func (Option[T]) ToPtr added in v0.18.2

func (o Option[T]) ToPtr() *T

ToPtr returns value if present or a nil pointer.

func (*Option[T]) UnmarshalBinary

func (o *Option[T]) UnmarshalBinary(data []byte) error

UnmarshalBinary is the interface implemented by an object that can unmarshal a binary representation of itself.

func (*Option[T]) UnmarshalJSON

func (o *Option[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes Option from json.

func (*Option[T]) UnmarshalText

func (o *Option[T]) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Option[T]) Value

func (o Option[T]) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type StackTrace added in v0.23.1

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

func (StackTrace) Format added in v0.23.1

func (st StackTrace) Format(s fmt.State, verb rune)

Format formats the stack of Frames according to the fmt.Formatter interface.

%s	lists source files for each Frame in the stack
%v	lists the source file and line number for each Frame in the stack

Format accepts flags that alter the printing of some verbs, as follows:

%+v   Prints filename, function, and line number for each Frame in the stack.

Jump to

Keyboard shortcuts

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