Documentation ¶
Overview ¶
Package base 基础类型.
包括 Map, Set, Slice, JSNumber.
Index ¶
- Constants
- Variables
- func All[E comparable](slice, elems []E) bool
- func Any[E comparable](slice, elems []E) bool
- func Append[S ~[]E, E any](max int, slice S, elems ...E) S
- func Bytes2Number[T constraints.Integer | constraints.Float](data []byte) T
- func Chunk[Elem any](slice []Elem, size int) [][]Elem
- func Conversion[T any](obj any, must bool) T
- func Count[E comparable](elems []E, elem E) int
- func Counts[E comparable](slice, sub []E) int
- func Del[E comparable](slice []E, elems ...E) []E
- func DelAll[E comparable](slice []E, elems ...E) []E
- func Errors(errs ...error) error
- func Filter[T any](elems []T, condition func(T) bool) []T
- func FormatFloat[T constraints.Float | constraints.Integer](num T, prec int) string
- func Has[E comparable](elems []E, elem E) booldeprecated
- func Index[T comparable](slices, sub []T) int
- func Itoa[T constraints.Integer | constraints.Float](num T) string
- func JSNumber[Num constraints.Integer | constraints.Float](num Num) Num
- func Join[E any](slice []E, sep string) string
- func LevenshteinDistance(str1, str2 string) int
- func Max[Num constraints.Ordered](nums ...Num) Num
- func Min[Num constraints.Ordered](nums ...Num) Num
- func Must(err error)
- func Must1[T any](t T, err error) T
- func Must2[T1, T2 any](t1 T1, t2 T2, err error) (T1, T2)
- func Must3[T1, T2, T3 any](t1 T1, t2 T2, t3 T3, err error) (T1, T2, T3)
- func Number2Bytes[T constraints.Integer | constraints.Float](num T) []byte
- func Panic(err error)deprecated
- func Panic1[T any](t T, err error) Tdeprecated
- func ParseFloat[T constraints.Float](str string) (T, error)
- func ParseInteger[T constraints.Integer](str string) (T, error)
- func ParseIntegerAny[T constraints.Integer](elem any) (T, error)
- func Pass(err error, passErrors ...error)
- func Pass1[T any](param T, err error, passErrors ...error) T
- func Pass2[T1, T2 any](param1 T1, param2 T2, err error, passErrors ...error) (T1, T2)
- func Pass3[T1, T2, T3 any](param1 T1, param2 T2, param3 T3, err error, passErrors ...error) (T1, T2, T3)
- func Recover(call func(error))
- func Replace[E comparable](slice, sub, newSub []E, num int) []E
- func ReplaceAll[E comparable](slice, sub, newSub []E) []E
- func Reverse[E any](slice []E)
- func Round[I constraints.Integer, F constraints.Float](float F) I
- func Sample[E any](slice []E) E
- func Shuffle[E any](slice []E) []E
- func SliceMap[S, T any](elems []S, change func(S) T) []T
- func Sorts[T constraints.Ordered, A any](keys []T, slices ...[]A)
- func Split(str string, seps ...rune) []string
- func SplitFunc(str string, splitFunc func(rune) bool) []string
- func String2Map(split, equal string, elems ...string) map[string]string
- func Sub[Elem any](slice []Elem, startAndEnd ...int) []Elem
- func Tag2Map(tag string) map[string]string
- func Unique[E comparable](slice []E) []E
- func Values[M ~map[K]V, K comparable, V any](m M, keys []K) []V
- func Wildcard(str string, pattern string) bool
- type Map
- func (p Map[K, V]) All(keys ...K) bool
- func (p Map[K, V]) Any(keys ...K) bool
- func (p Map[K, V]) Clear()deprecated
- func (p Map[K, V]) Clone() Map[K, V]deprecated
- func (p Map[K, V]) Copy(dst Map[K, V])deprecated
- func (p Map[K, V]) Del(keys ...K)
- func (p Map[K, V]) DelMap(elems ...Map[K, V])
- func (p Map[K, V]) Has(key K) bool
- func (p Map[K, V]) Keys() []Kdeprecated
- func (p Map[K, V]) Put(elems ...Map[K, V])
- func (p Map[K, V]) String() string
- func (p Map[K, V]) Values() []Vdeprecated
- func (p Map[K, V]) ValuesByKeys(keys []K) []V
- type Set
- func (set Set[K]) Add(elems ...K) Set[K]
- func (set Set[K]) AddSet(elems ...Set[K]) Set[K]
- func (set Set[K]) All(keys ...K) bool
- func (set Set[K]) Any(keys ...K) bool
- func (set Set[K]) Del(keys ...K) Set[K]
- func (set Set[K]) DelSet(elems ...Set[K]) Set[K]
- func (set Set[K]) Has(key K) bool
- func (set Set[K]) Join(sep string) string
- func (set Set[K]) Slice() []K
- func (set Set[K]) String() string
- type Slice
- func (p *Slice[T]) Add(elems ...T)
- func (p Slice[T]) All(elems ...T) booldeprecated
- func (p Slice[T]) Any(elems ...T) booldeprecated
- func (p *Slice[T]) Clean()
- func (p *Slice[T]) Clip()deprecated
- func (p Slice[T]) Clone() Slice[T]deprecated
- func (p Slice[T]) Compare(dst Slice[T]) intdeprecated
- func (p Slice[T]) Contains(elem T) booldeprecated
- func (p Slice[T]) Count(elem T) intdeprecated
- func (p Slice[T]) Counts(elems []T) int
- func (p *Slice[T]) Del(elems ...T) Slice[T]deprecated
- func (p *Slice[T]) DelAll(elems ...T) Slice[T]deprecated
- func (p *Slice[T]) Delete(start, end int)deprecated
- func (p Slice[T]) Equal(dst Slice[T]) booldeprecated
- func (p *Slice[T]) Grow(size int)deprecated
- func (p Slice[T]) Has(elem T) booldeprecated
- func (p Slice[T]) Index(elem T) intdeprecated
- func (p Slice[T]) Indexs(elems []T) intdeprecated
- func (p *Slice[T]) Insert(index int, elems ...T)deprecated
- func (p Slice[T]) Join(sep string) stringdeprecated
- func (p Slice[T]) Len() int
- func (p Slice[T]) Less(i, j int) bool
- func (p *Slice[T]) Push(elems ...T)
- func (p *Slice[T]) Replace(oldSlice, newSlice []T, num int) Slice[T]deprecated
- func (p Slice[T]) ReplaceAll(oldSlice, newSlice []T) Slice[T]deprecated
- func (p Slice[T]) String() string
- func (p Slice[T]) Swap(i, j int)
- func (p *Slice[T]) Unique()deprecated
Examples ¶
Constants ¶
View Source
const ( Two = 2 There = 3 Four = 4 Five = 5 Six = 6 Seven = 7 Eight = 8 Nine = 9 Ten = 10 Sixteen = 16 SixtyFour = 64 Hundred = 100 Million = 1e6 HundredMillion = 1e8 Billion = 1e9 )
View Source
const SepInitialisms = rune(-3)
SepInitialisms 首字母缩写分割.
Variables ¶
View Source
var ( ErrConversion = errors.New("conversion bool is false") ErrNotNum = errors.New("不是数值类型") ErrEmpty = errors.New("切片为空") )
View Source
var CommonInitialisms = NewSet(
"ACL",
"API",
"ASCII",
"CPU",
"CSS",
"DNS",
"EOF",
"GUID",
"HTML",
"HTTP",
"HTTPS",
"ID",
"IP",
"JSON",
"LHS",
"QPS",
"RAM",
"RHS",
"RPC",
"SLA",
"SMTP",
"SQL",
"SSH",
"TCP",
"TLS",
"TTL",
"UDP",
"UI",
"UID",
"UUID",
"URI",
"URL",
"UTF8",
"VM",
"XML",
"XMPP",
"XSRF",
"XSS",
)
nolint
View Source
var None struct{} // nolint
None 无值.
Functions ¶
func Bytes2Number ¶
func Bytes2Number[T constraints.Integer | constraints.Float](data []byte) T
nolint
func Conversion ¶ added in v0.2.17
Conversion 类型转换,must是否必须成功.
func Counts ¶ added in v0.3.22
func Counts[E comparable](slice, sub []E) int
func Del ¶ added in v0.3.22
func Del[E comparable](slice []E, elems ...E) []E
func DelAll ¶ added in v0.3.22
func DelAll[E comparable](slice []E, elems ...E) []E
func FormatFloat ¶
func FormatFloat[T constraints.Float | constraints.Integer](num T, prec int) string
FormatFloat 浮点数格式化成字符串.
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { fmt.Println(base.FormatFloat(3, 3)) fmt.Println(base.FormatFloat(3.14, 3)) }
Output: 3 3.14
func Has
deprecated
added in
v0.1.5
func Itoa ¶
func Itoa[T constraints.Integer | constraints.Float](num T) string
Itoa 数值装换成字符串.
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { fmt.Println(base.Itoa(3)) fmt.Println(base.Itoa(3.14)) }
Output: 3 3
func JSNumber ¶
func JSNumber[Num constraints.Integer | constraints.Float](num Num) Num
JSNumber 转换成兼容JS的数值.
func LevenshteinDistance ¶ added in v0.3.22
LevenshteinDistance 编辑距离.
func Number2Bytes ¶
func Number2Bytes[T constraints.Integer | constraints.Float](num T) []byte
func ParseFloat ¶
func ParseFloat[T constraints.Float](str string) (T, error)
ParseFloat 字符串转换成浮点数.
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { fmt.Println(base.Must1(base.ParseFloat[float32]("3.14"))) }
Output: 3.14
func ParseInteger ¶
func ParseInteger[T constraints.Integer](str string) (T, error)
ParseInteger 字符串转换成整数. nolint
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { // 字符串转换成整数 fmt.Println(base.Must1(base.ParseInteger[int]("3"))) }
Output: 3
func ParseIntegerAny ¶ added in v0.3.22
func ParseIntegerAny[T constraints.Integer](elem any) (T, error)
ParseIntegerAny 任意类型转换成数值. nolint
func Pass1 ¶
Pass1 忽略 error, 返回1个结果. 推荐 value, _ := FuncTwoReturn() 可选 FuncOneParam(base.Pass1(FuncTwoReturn())).
func Pass3 ¶
func Pass3[T1, T2, T3 any](param1 T1, param2 T2, param3 T3, err error, passErrors ...error) (T1, T2, T3)
Pass3 忽略 error, 返回3个结果.
func Replace ¶ added in v0.3.22
func Replace[E comparable](slice, sub, newSub []E, num int) []E
Replace 替换.
func ReplaceAll ¶ added in v0.3.22
func ReplaceAll[E comparable](slice, sub, newSub []E) []E
ReplaceAll 全部替换.
func Shuffle ¶ added in v0.3.22
func Shuffle[E any](slice []E) []E
Shuffle 打乱切片.
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { fmt.Println(base.Shuffle([]int{1, 2, 3, 4, 5, 6})) }
Output:
func SliceMap ¶ added in v0.2.19
func SliceMap[S, T any](elems []S, change func(S) T) []T
SliceMap 切片转换.
func Sorts ¶ added in v0.2.10
func Sorts[T constraints.Ordered, A any](keys []T, slices ...[]A)
Sorts keys 和 slices 都根据keys排序.
func String2Map ¶ added in v0.3.21
String2Map 字符串转换成 map.
func Sub ¶ added in v0.3.21
Sub 切片截取.
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { fmt.Println(base.Sub([]int{1, 2, 3}, -1)) fmt.Println(base.Sub([]int{1, 2, 3, 4, 5}, 1, -2)) }
Output: [3] [2 3]
func Values ¶ added in v0.2.17
func Values[M ~map[K]V, K comparable, V any](m M, keys []K) []V
Values 根据keys获取map的values.
Types ¶
type Map ¶
type Map[K comparable, V any] map[K]V
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { map1 := base.NewMap[int, int]() map1[1] = 2 map1[3] = 5 fmt.Println(len(map1)) }
Output: 2
func NewMap ¶
func NewMap[K comparable, V any]() Map[K, V]
func NewMapSameValue ¶ added in v0.2.13
func NewMapSameValue[K comparable, V any](value V, keys ...K) Map[K, V]
func (Map[K, V]) ValuesByKeys ¶ added in v0.2.19
func (p Map[K, V]) ValuesByKeys(keys []K) []V
ValuesByKey 根据key获取value.
type Set ¶
type Set[K comparable] map[K]struct{}
Set 通用set.
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { set := base.NewSet(1, 2, 2) set.Add(3) fmt.Println(len(set)) fmt.Println(set.Any(7, 2)) fmt.Println(set.All(7, 2)) set.Del(1) fmt.Println(len(set)) }
Output: 3 true false 2
type Slice ¶
type Slice[T constraints.Ordered] []T
Slice 切片.
Example ¶
package main import ( "fmt" "sort" "github.com/xuender/oils/base" ) func main() { str := base.NewSlice("c", "a") sort.Sort(str) fmt.Println(str[0]) fmt.Println(str[1]) fmt.Println(str.All("a", "b")) fmt.Println(str.Any("a", "b")) fmt.Println(str.Has("b")) fmt.Println(str.Index("c")) fmt.Println(str.Index("b")) fmt.Println(str) }
Output: a c false true false 1 -1 Slice[a c]
func (Slice[T]) ReplaceAll
deprecated
Source Files ¶
Click to show internal directories.
Click to hide internal directories.