algostruct

package
v0.0.0-...-4d13243 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type INullable

type INullable[T any] interface {
	// 实现了数据库驱动的 Value 扫描功能,允许将数据库中的值转换为Nullable类型
	sql.Scanner

	// 实现了数据库驱动的 Value 接口,用于将Nullable类型转换为数据库驱动期望的值
	driver.Valuer

	// 实现了json.Marshaler接口,用于将Nullable类型值转换为JSON格式
	json.Marshaler

	// 实现了json.Unmarshaler接口,用于将JSON格式的数据转换为Nullable类型值
	json.Unmarshaler

	fmt.Stringer

	//
	// 返回值:
	//   bool: 如果当前对象为零值,则返回true;否则返回false。
	IsZero() bool

	// 用于获取Nullable类型内部存储的值
	Get() (T, bool)

	// 用于获取Nullable类型内部存储的值,如果未设置,则返回默认值
	GetOrElse(defaultVal T) T

	// 用于判断 Nullable 类型内部的值是否未设置
	IsNull() bool
	// 用于判断 Nullable 类型内部的值是否已设置
	IsNotNull() bool

	// 用于设置某个类型的值
	Set(val T)

	// 用于将值设置为NULL
	SetNull()
}

INullable 是一个泛型接口,表示可以表示任何类型的 NULL 值

type Nullable

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

func NewNullable

func NewNullable[T any](value T) Nullable[T]

func (*Nullable[T]) Get

func (n *Nullable[T]) Get() (T, bool)

Get 获取值及其有效性状态

func (*Nullable[T]) GetOrElse

func (n *Nullable[T]) GetOrElse(defaultVal T) T

GetOrElse 获取值,如果无效则返回默认值

func (*Nullable[T]) IsNotNull

func (n *Nullable[T]) IsNotNull() bool

IsNotNull 检查值是否不为 NULL

func (*Nullable[T]) IsNull

func (n *Nullable[T]) IsNull() bool

IsNull 检查值是否为 NULL

func (*Nullable[T]) IsZero

func (n *Nullable[T]) IsZero() bool

IsZero 判断 nullable 类型的值是否为零值。 此方法主要用于检查类型 T 的值是否被设置(即是否存在)。 返回值为布尔类型,如果值未被设置,则返回 true,否则返回 false。

func (*Nullable[T]) MarshalJSON

func (n *Nullable[T]) MarshalJSON() ([]byte, error)

MarshalJSON 实现 json.Marshaler 接口

func (*Nullable[T]) Scan

func (n *Nullable[T]) Scan(src any) error

Scan 实现 sql.Scanner 接口

func (*Nullable[T]) Set

func (n *Nullable[T]) Set(val T)

Set 设置值

func (*Nullable[T]) SetNull

func (n *Nullable[T]) SetNull()

SetNull 将值设置为 NULL

func (*Nullable[T]) String

func (n *Nullable[T]) String() string

String 实现 fmt.Stringer 接口

func (*Nullable[T]) UnmarshalJSON

func (n *Nullable[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON 实现 json.Unmarshaler 接口

func (*Nullable[T]) Value

func (n *Nullable[T]) Value() (driver.Value, error)

Value 实现 driver.Valuer 接口

type Queue

type Queue[T any] interface {
	//入队
	Enqueue(value T)
	//出队
	Dequeue() (T, bool)
	//判空
	IsEmpty() bool
	//迭代器 (Go 1.23+)
	Iter() iter.Seq[T]
}

线程安全队列

func NewQueue

func NewQueue[T any]() Queue[T]

NewQueue creates a new empty queue

type Set

type Set[T comparable] interface {
	//添加元素到集合
	Add(item T) bool
	// 从集合中移除元素
	Remove(item T) bool
	//检查集合是否包含元素
	Contains(item T) bool
	//返回集合大小
	Size() int
	//清空集合
	Clear()
	//返回集合中所有元素的切片
	Items() []T
	//迭代器 (Go 1.23+)
	Iter() iter.Seq[T]
}

func NewSet

func NewSet[T comparable]() Set[T]

创建一个新的线程安全的泛型 Set

Source Files

  • nullable.go
  • queue.go
  • set.go

Jump to

Keyboard shortcuts

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