Documentation
¶
Overview ¶
Package gomad 定义 Option 与 Result 的共享值类型。
通常应从子包 option 和 result 使用这些类型;根包用于消除两个类型互相 转换时的循环依赖。
Index ¶
- type Option
- func (opt Option[T]) AndThen[U any](transform func(T) Option[U]) Option[U]
- func (opt Option[T]) Expect(message string) T
- func (opt Option[T]) Filter(predicate func(T) bool) Option[T]
- func (opt Option[T]) Flatten() T
- func (opt Option[T]) Get() (T, bool)
- func (opt Option[T]) Inspect(inspect func(T)) Option[T]
- func (opt Option[T]) IsNone() bool
- func (opt Option[T]) IsSome() bool
- func (opt Option[T]) Iter() iterator.Iterator[T]
- func (opt Option[T]) Map[U any](transform func(T) U) Option[U]
- func (opt Option[T]) MarshalJSON() ([]byte, error)
- func (opt Option[T]) OkOr[E any](err E) Result[T, E]
- func (opt Option[T]) OkOrElse[E any](fallback func() E) Result[T, E]
- func (opt Option[T]) OrElse(fallback func() Option[T]) Option[T]
- func (opt *Option[T]) Scan(source any) error
- func (opt *Option[T]) UnmarshalJSON(data []byte) error
- func (opt Option[T]) Unwarp() T
- func (opt Option[T]) Unwrap() T
- func (opt Option[T]) UnwrapOr(fallback T) T
- func (opt Option[T]) UnwrapOrElse(fallback func() T) T
- func (opt Option[T]) Value() (driver.Value, error)
- type Result
- func (result Result[T, E]) AndThen[U any](transform func(T) Result[U, E]) Result[U, E]
- func (result Result[T, E]) Err() Option[E]
- func (result Result[T, E]) Expect(message string) T
- func (result Result[T, E]) ExpectErr(message string) E
- func (result Result[T, E]) ExpectMust[U any](f func(T) (U, error), message string) Result[U, error]
- func (result Result[T, E]) Get() (T, E, bool)
- func (result Result[T, E]) Inspect(inspect func(T)) Result[T, E]
- func (result Result[T, E]) InspectErr(inspect func(E)) Result[T, E]
- func (result Result[T, E]) IntoError() error
- func (result Result[T, E]) IsErr() bool
- func (result Result[T, E]) IsOk() bool
- func (result Result[T, E]) Iter() iterator.Iterator[T]
- func (result Result[T, E]) Map[U any](transform func(T) U) Result[U, E]
- func (result Result[T, E]) MapErr[F any](transform func(E) F) Result[T, F]
- func (result Result[T, E]) MarshalJSON() ([]byte, error)
- func (result Result[T, E]) Must[U any](f func(T) (U, error)) Result[U, error]
- func (result Result[T, E]) Ok() Option[T]
- func (result Result[T, E]) OrElse[F any](fallback func(E) Result[T, F]) Result[T, F]
- func (result *Result[T, E]) UnmarshalJSON(data []byte) error
- func (result Result[T, E]) Unwrap() T
- func (result Result[T, E]) UnwrapErr() E
- func (result Result[T, E]) UnwrapOr(fallback T) T
- func (result Result[T, E]) UnwrapOrElse(fallback func(E) T) T
- func (result Result[T, E]) Wrap(message string) Result[T, error]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option 表示一个可能存在的值:Some 包含值,None 不包含值。 Option 的零值等价于 None。
func (Option[T]) Flatten ¶
func (opt Option[T]) Flatten() T
Flatten 去掉一层 Option 嵌套。 泛型代码中建议优先使用 option.Flatten,以获得更严格的类型约束。
func (Option[T]) MarshalJSON ¶
MarshalJSON 将 Some 编码为内部值,将 None 编码为 null。
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON 将 null 解码为 None,其他 JSON 值解码为 Some。
func (Option[T]) Unwarp ¶
func (opt Option[T]) Unwarp() T
Unwarp 为早期版本的拼写错误保留源码兼容性。 Deprecated: 请使用 Unwrap。
func (Option[T]) UnwrapOr ¶
func (opt Option[T]) UnwrapOr(fallback T) T
UnwrapOr 返回 Some 中的值,None 则返回 fallback。
func (Option[T]) UnwrapOrElse ¶
func (opt Option[T]) UnwrapOrElse(fallback func() T) T
UnwrapOrElse 返回 Some 中的值,None 则调用 fallback 生成默认值。
type Result ¶
type Result[T, E any] struct { // contains filtered or unexported fields }
Result 表示一次可能成功或失败的计算:Ok 包含成功值,Err 包含错误值。 Result 的零值是包含 E 零值的 Err。
func (Result[T, E]) ExpectMust ¶ added in v0.3.0
ExpectMust Must+自定义错误內容
func (Result[T, E]) InspectErr ¶
InspectErr 在 Err 分支执行只读回调,并返回原 Result。
func (Result[T, E]) MarshalJSON ¶
MarshalJSON 将 Result 编码为 {"ok": value} 或 {"err": err}。
func (*Result[T, E]) UnmarshalJSON ¶
UnmarshalJSON 从只包含 ok 或 err 的 JSON 对象解码 Result。
func (Result[T, E]) Unwrap ¶
func (result Result[T, E]) Unwrap() T
Unwrap 返回 Ok 中的值;对 Err 调用时会触发 panic。
func (Result[T, E]) UnwrapErr ¶
func (result Result[T, E]) UnwrapErr() E
UnwrapErr 返回 Err 中的错误值;对 Ok 调用时会触发 panic。
func (Result[T, E]) UnwrapOr ¶
func (result Result[T, E]) UnwrapOr(fallback T) T
UnwrapOr 返回 Ok 中的值,Err 则返回 fallback。
func (Result[T, E]) UnwrapOrElse ¶
func (result Result[T, E]) UnwrapOrElse(fallback func(E) T) T
UnwrapOrElse 返回 Ok 中的值,Err 则调用 fallback 生成默认值。
Directories
¶
| Path | Synopsis |
|---|---|
|
Package iterator 提供 Option 与 Result 使用的小型值迭代器。
|
Package iterator 提供 Option 与 Result 使用的小型值迭代器。 |
|
Package option 提供零分配的 Option 类型及其常用辅助函数。
|
Package option 提供零分配的 Option 类型及其常用辅助函数。 |
|
Package result 提供显式表示成功与失败的 Result 类型及辅助函数。
|
Package result 提供显式表示成功与失败的 Result 类型及辅助函数。 |