o

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: Apache-2.0 Imports: 7 Imported by: 1

README

go-object-util

使用go-object风格封装的工具.

go-object

https://github.com/jishaocong0910/go-object

枚举

使用例子

package main

import (
	"fmt"
	o "github.com/jishaocong0910/go-object-util"
)

// 声明枚举元素
type ImageType struct {
	*o.EnumElem__
	MIME string
}

// 声明枚举集合
type _ImageType struct {
	*o.Enum__[ImageType]
	JPG,
	PNG,
	GIF ImageType
}

// 自定义元素查找
func (i _ImageType) OfMime(mine string) ImageType {
	for _, e := range i.Elems() {
		if e.MIME == mine {
			return e
		}
	}
	return i.Undefined()
}

// 创建枚举变量
var ImageType_ = o.NewEnum[ImageType](_ImageType{
	JPG: ImageType{MIME: "image/jpeg"},
	PNG: ImageType{MIME: "image/png"},
	GIF: ImageType{MIME: "image/gif"},
})

func main() {
	fmt.Println(ImageType_.JPG.ID())                           // ID为枚举集合中的变量名
	fmt.Println(ImageType_.OfId("JPG").ID())                   // 内置的查找方法
	fmt.Println(ImageType_.OfIdIgnoreCase("png").ID())         // 内置的查找方法
	fmt.Println(ImageType_.OfMime("image/jpeg").ID())          // 自定义查找方法
	fmt.Println(ImageType_.OfId("BMP").Undefined())            // 判断枚举元素是否存在
	fmt.Println(ImageType_.Is(ImageType_.JPG, ImageType_.PNG)) // 判断枚举元素是否相等

	// switch块中使用枚举元素的ID判断
	i := ImageType_.OfMime("image/webp")
	switch i.ID() {
	case ImageType_.JPG.ID():
		fmt.Println("is jpg")
	case ImageType_.PNG.ID():
		fmt.Println("is png")
	case ImageType_.GIF.ID():
		fmt.Println("is gif")
	default:
		fmt.Println("unknown image type")
	}
}

集合

  • Map
    • NewMap[K,V](普通Map)
    • NewStrKeyMap[K,V](字符串key的Map,可设置key不区分大小写)
    • NewSyncMap[K,V](同步Map)
  • Set
    • NewSet[T](普通Set)
    • NewStrSet[T](字符串Set,可设置不区分大小写)
    • NewSyncSet[T](同步Set)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNull

func IsNull(a any) bool

func NewEnum

func NewEnum[E enumElem_, ES enum_[E]](e ES) ES

func NotNull

func NotNull(a any) bool

Types

type Entry

type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

type EnumElem__

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

func (*EnumElem__) ID

func (this *EnumElem__) ID() string

ID 枚举值ID,值为枚举集合中的字段名

func (*EnumElem__) Undefined

func (this *EnumElem__) Undefined() bool

Undefined 是否未定义的枚举

type Enum__

type Enum__[E enumElem_] struct {
	// contains filtered or unexported fields
}

func (*Enum__[E]) Elems

func (this *Enum__[E]) Elems() []E

Elems 返回所有枚举值

func (*Enum__[E]) Ids added in v1.0.1

func (this *Enum__[E]) Ids() []string

Elems 返回所有枚举ID

func (*Enum__[E]) Is

func (this *Enum__[E]) Is(source E, targets ...E) bool

Is 判断是否存在指定枚举值

func (*Enum__[E]) Not

func (this *Enum__[E]) Not(source E, targets ...E) bool

Not 与Is方法相反

func (*Enum__[E]) OfId

func (this *Enum__[E]) OfId(id string) (e E)

OfId 查找ID对应枚举值

func (*Enum__[E]) OfIdIgnoreCase

func (this *Enum__[E]) OfIdIgnoreCase(id string) (e E)

OfIdIgnoreCase 查找ID对应枚举值,不区分大小写

func (*Enum__[E]) Undefined

func (this *Enum__[E]) Undefined() E

Undefined 返回一个未定义的枚举值

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewMap

func NewMap[K comparable, V any]() *Map[K, V]

func (*Map[K, V]) ContainsAnyKeys

func (this *Map[K, V]) ContainsAnyKeys(ks ...K) bool

func (*Map[K, V]) ContainsKeys

func (this *Map[K, V]) ContainsKeys(ks ...K) bool

func (*Map[K, V]) Empty

func (this *Map[K, V]) Empty() bool

func (*Map[K, V]) Get

func (this *Map[K, V]) Get(k K) (v V)

func (*Map[K, V]) GetEntry

func (this *Map[K, V]) GetEntry(k K) *Entry[K, V]

func (*Map[K, V]) GetIfAbsent

func (this *Map[K, V]) GetIfAbsent(k K, f func(k K) V) (v V)

func (*Map[K, V]) Keys

func (this *Map[K, V]) Keys() []K

func (*Map[K, V]) Len

func (this *Map[K, V]) Len() int64

func (*Map[K, V]) NotEmpty

func (this *Map[K, V]) NotEmpty() bool

func (*Map[K, V]) Put

func (this *Map[K, V]) Put(k K, v V)

func (*Map[K, V]) PutAll

func (this *Map[K, V]) PutAll(other Map_[K, V])

func (*Map[K, V]) Range

func (this *Map[K, V]) Range(f func(k K, v V))

func (*Map[K, V]) Raw

func (this *Map[K, V]) Raw() map[K]V

func (*Map[K, V]) Remove

func (this *Map[K, V]) Remove(k K) bool

func (*Map[K, V]) RemoveAll

func (this *Map[K, V]) RemoveAll(ks ...K)

func (*Map[K, V]) Values

func (this *Map[K, V]) Values() []V

type Map_

type Map_[K comparable, V any] interface {
	Put(k K, v V)
	PutAll(other Map_[K, V])
	GetEntry(k K) *Entry[K, V]
	Get(k K) (v V)
	GetIfAbsent(k K, f func(k K) V) (v V)
	Remove(k K) bool
	RemoveAll(ks ...K)
	ContainsKeys(ks ...K) bool
	ContainsAnyKeys(ks ...K) bool
	Keys() []K
	Values() []V
	Len() int64
	Empty() bool
	NotEmpty() bool
	Raw() map[K]V
	Range(f func(k K, v V))
	// contains filtered or unexported methods
}

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSet

func NewSet[T comparable](es ...T) *Set[T]

func (Set) Add

func (this Set) Add(es ...T)

func (Set) AddSet

func (this Set) AddSet(other Set_[T])

func (Set) Contains

func (this Set) Contains(ts ...T) bool

func (Set) ContainsAny

func (this Set) ContainsAny(es ...T) bool

func (Set) Empty

func (this Set) Empty() bool

func (Set) Len

func (this Set) Len() int64

func (Set) NotEmpty

func (this Set) NotEmpty() bool

func (Set) Range

func (this Set) Range(f func(e T))

func (Set) Raw

func (this Set) Raw() []T

func (Set) Remove

func (this Set) Remove(e T) bool

func (Set) RemoveAll

func (this Set) RemoveAll(e ...T)

type Set_

type Set_[T comparable] interface {
	Add(es ...T)
	AddSet(other Set_[T])
	Remove(e T) bool
	RemoveAll(e ...T)
	Contains(es ...T) bool
	ContainsAny(es ...T) bool
	Len() int64
	Empty() bool
	NotEmpty() bool
	Raw() []T
	Range(f func(e T))
	// contains filtered or unexported methods
}

type StrKeyMap

type StrKeyMap[V any] struct {
	*Map[string, V]
	// contains filtered or unexported fields
}

func NewStrKeyMap

func NewStrKeyMap[V any](caseSensitive bool) *StrKeyMap[V]

type StrSet

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

func NewStrSet

func NewStrSet(caseSensitive bool, es ...string) *StrSet

func (StrSet) Add

func (this StrSet) Add(es ...T)

func (StrSet) AddSet

func (this StrSet) AddSet(other Set_[T])

func (StrSet) Contains

func (this StrSet) Contains(ts ...T) bool

func (StrSet) ContainsAny

func (this StrSet) ContainsAny(es ...T) bool

func (StrSet) Empty

func (this StrSet) Empty() bool

func (StrSet) Len

func (this StrSet) Len() int64

func (StrSet) NotEmpty

func (this StrSet) NotEmpty() bool

func (StrSet) Range

func (this StrSet) Range(f func(e T))

func (StrSet) Raw

func (this StrSet) Raw() []T

func (StrSet) Remove

func (this StrSet) Remove(e T) bool

func (StrSet) RemoveAll

func (this StrSet) RemoveAll(e ...T)

type SyncMap

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewSyncMap

func NewSyncMap[K comparable, V any]() *SyncMap[K, V]

func (*SyncMap[K, V]) ContainsAnyKeys

func (this *SyncMap[K, V]) ContainsAnyKeys(ks ...K) bool

func (*SyncMap[K, V]) ContainsKeys

func (this *SyncMap[K, V]) ContainsKeys(ks ...K) bool

func (*SyncMap[K, V]) Empty

func (this *SyncMap[K, V]) Empty() bool

func (*SyncMap[K, V]) Get

func (this *SyncMap[K, V]) Get(k K) (v V)

func (*SyncMap[K, V]) GetEntry

func (this *SyncMap[K, V]) GetEntry(k K) *Entry[K, V]

func (*SyncMap[K, V]) GetIfAbsent

func (this *SyncMap[K, V]) GetIfAbsent(k K, f func(k K) V) (v V)

func (*SyncMap[K, V]) Keys

func (this *SyncMap[K, V]) Keys() []K

func (*SyncMap[K, V]) Len

func (this *SyncMap[K, V]) Len() int64

func (*SyncMap[K, V]) NotEmpty

func (this *SyncMap[K, V]) NotEmpty() bool

func (*SyncMap[K, V]) Put

func (this *SyncMap[K, V]) Put(k K, v V)

func (*SyncMap[K, V]) PutAll

func (this *SyncMap[K, V]) PutAll(other Map_[K, V])

func (*SyncMap[K, V]) Range

func (this *SyncMap[K, V]) Range(f func(k K, v V))

func (*SyncMap[K, V]) Raw

func (this *SyncMap[K, V]) Raw() map[K]V

func (*SyncMap[K, V]) Remove

func (this *SyncMap[K, V]) Remove(k K) bool

func (*SyncMap[K, V]) RemoveAll

func (this *SyncMap[K, V]) RemoveAll(ks ...K)

func (*SyncMap[K, V]) Values

func (this *SyncMap[K, V]) Values() []V

type SyncSet

type SyncSet[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSyncSet

func NewSyncSet[T comparable](es ...T) *SyncSet[T]

func (SyncSet) Add

func (this SyncSet) Add(es ...T)

func (SyncSet) AddSet

func (this SyncSet) AddSet(other Set_[T])

func (SyncSet) Contains

func (this SyncSet) Contains(ts ...T) bool

func (SyncSet) ContainsAny

func (this SyncSet) ContainsAny(es ...T) bool

func (SyncSet) Empty

func (this SyncSet) Empty() bool

func (SyncSet) Len

func (this SyncSet) Len() int64

func (SyncSet) NotEmpty

func (this SyncSet) NotEmpty() bool

func (SyncSet) Range

func (this SyncSet) Range(f func(e T))

func (SyncSet) Raw

func (this SyncSet) Raw() []T

func (SyncSet) Remove

func (this SyncSet) Remove(e T) bool

func (SyncSet) RemoveAll

func (this SyncSet) RemoveAll(e ...T)

Jump to

Keyboard shortcuts

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