base

package
v0.5.40 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: MIT Imports: 12 Imported by: 15

Documentation

Overview

Package base 基础类型.

包括 Map, Set, Slice, JSNumber.

Index

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 All added in v0.3.22

func All[E comparable](slice, elems []E) bool

All 全部包含.

func Any added in v0.3.22

func Any[E comparable](slice, elems []E) bool

Any 任意包含.

func Append added in v0.2.10

func Append[S ~[]E, E any](max int, slice S, elems ...E) S

Append 追加,max最大尺寸,max小于0不限制尺寸.

func Bytes2Number

func Bytes2Number[T constraints.Integer | constraints.Float](data []byte) T

nolint

func Chunk added in v0.3.21

func Chunk[Elem any](slice []Elem, size int) [][]Elem

Chunk 切片分块.

func Conversion added in v0.2.17

func Conversion[T any](obj any, must bool) T

Conversion 类型转换,must是否必须成功.

func Count added in v0.1.5

func Count[E comparable](elems []E, elem E) int

Count 统计元素数量.

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 Errors added in v0.2.16

func Errors(errs ...error) error

func Filter added in v0.1.8

func Filter[T any](elems []T, condition func(T) bool) []T

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 Has[E comparable](elems []E, elem E) bool

Has 包含.

Deprecated: 使用 slices.Contains.

func Index added in v0.1.5

func Index[T comparable](slices, sub []T) int

Index 索引.

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 Join added in v0.3.22

func Join[E any](slice []E, sep string) string

Join 集合连接.

func LevenshteinDistance added in v0.3.22

func LevenshteinDistance(str1, str2 string) int

LevenshteinDistance 编辑距离.

func Max added in v0.1.9

func Max[Num constraints.Ordered](nums ...Num) Num

Max 最大值.

func Min added in v0.1.9

func Min[Num constraints.Ordered](nums ...Num) Num

Min 最小值.

func Must added in v0.2.10

func Must(err error)

Must error 必须是nil.

func Must1 added in v0.2.10

func Must1[T any](t T, err error) T

Must1 1个返回值,error 必须是nil.

func Must2 added in v0.2.10

func Must2[T1, T2 any](t1 T1, t2 T2, err error) (T1, T2)

Must2 2个返回值, error 必须是nil.

func Must3 added in v0.2.10

func Must3[T1, T2, T3 any](t1 T1, t2 T2, t3 T3, err error) (T1, T2, T3)

Must3 3个返回值, error 必须是nil.

func Number2Bytes

func Number2Bytes[T constraints.Integer | constraints.Float](num T) []byte

func Panic deprecated

func Panic(err error)

Panic error引发恐慌.

Deprecated: 使用Must.

func Panic1 deprecated

func Panic1[T any](t T, err error) T

Panic1 恐慌1.

Deprecated: 使用Must1.

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 Pass

func Pass(err error, passErrors ...error)

Pass 忽略 error.

func Pass1

func Pass1[T any](param T, err error, passErrors ...error) T

Pass1 忽略 error, 返回1个结果. 推荐 value, _ := FuncTwoReturn() 可选 FuncOneParam(base.Pass1(FuncTwoReturn())).

func Pass2

func Pass2[T1, T2 any](param1 T1, param2 T2, err error, passErrors ...error) (T1, T2)

Pass2 忽略 error, 返回2个结果.

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 Recover added in v0.2.15

func Recover(call func(error))

Recover 异常捕捉.

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 Reverse added in v0.3.22

func Reverse[E any](slice []E)

Reverse 反转切片.

func Round

func Round[I constraints.Integer, F constraints.Float](float F) I

Round 四舍五入.

func Sample added in v0.3.22

func Sample[E any](slice []E) E

Sample 获得一个随机元素.

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 Split added in v0.2.13

func Split(str string, seps ...rune) []string

Split 根据分割符拆分字符串. SepInitialisms 缩写子母拆分.

func SplitFunc added in v0.2.19

func SplitFunc(str string, splitFunc func(rune) bool) []string

SplitFunc 根据函数拆分字符串.

func String2Map added in v0.3.21

func String2Map(split, equal string, elems ...string) map[string]string

String2Map 字符串转换成 map.

func Sub added in v0.3.21

func Sub[Elem any](slice []Elem, startAndEnd ...int) []Elem

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 Tag2Map added in v0.3.21

func Tag2Map(tag string) map[string]string

Tag2Map stuct 的 Tag 转换成 map.

func Unique added in v0.3.22

func Unique[E comparable](slice []E) []E

Unique 去重.

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.

func Wildcard added in v0.3.22

func Wildcard(str string, pattern string) bool

Wildcard 字符串通配符匹配. pattern 支持 * 和 ?.

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]) All

func (p Map[K, V]) All(keys ...K) bool

func (Map[K, V]) Any

func (p Map[K, V]) Any(keys ...K) bool

func (Map[K, V]) Clear deprecated added in v0.1.5

func (p Map[K, V]) Clear()

Clear 清除.

Deprecated: 使用 maps.Clear.

func (Map[K, V]) Clone deprecated added in v0.1.5

func (p Map[K, V]) Clone() Map[K, V]

Clone 克隆.

Deprecated: 使用 maps.Clone.

func (Map[K, V]) Copy deprecated added in v0.1.5

func (p Map[K, V]) Copy(dst Map[K, V])

Copy 复制.

Deprecated: 使用 maps.Copy.

func (Map[K, V]) Del

func (p Map[K, V]) Del(keys ...K)

func (Map[K, V]) DelMap

func (p Map[K, V]) DelMap(elems ...Map[K, V])

func (Map[K, V]) Has

func (p Map[K, V]) Has(key K) bool

func (Map[K, V]) Keys deprecated

func (p Map[K, V]) Keys() []K

Keys 键切片.

Deprecated: 使用 maps.Keys.

func (Map[K, V]) Put

func (p Map[K, V]) Put(elems ...Map[K, V])

func (Map[K, V]) String

func (p Map[K, V]) String() string

String 字符串.

func (Map[K, V]) Values deprecated added in v0.1.5

func (p Map[K, V]) Values() []V

Values 值切片.

Deprecated: 使用 maps.Values.

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

func NewSet

func NewSet[K comparable](elems ...K) Set[K]

NewSet 新建set.

func (Set[K]) Add

func (set Set[K]) Add(elems ...K) Set[K]

Add 增加.

func (Set[K]) AddSet

func (set Set[K]) AddSet(elems ...Set[K]) Set[K]

AddSet 增加set.

func (Set[K]) All

func (set Set[K]) All(keys ...K) bool

All 包含全部.

func (Set[K]) Any

func (set Set[K]) Any(keys ...K) bool

Any 包含任意个.

func (Set[K]) Del

func (set Set[K]) Del(keys ...K) Set[K]

Del 删除.

func (Set[K]) DelSet

func (set Set[K]) DelSet(elems ...Set[K]) Set[K]

DelSet 删除set.

func (Set[K]) Has

func (set Set[K]) Has(key K) bool

Has 包含.

func (Set[K]) Join

func (set Set[K]) Join(sep string) string

Join 集合连接.

func (Set[K]) Slice

func (set Set[K]) Slice() []K

Slice 转换切片.

func (Set[K]) String

func (set Set[K]) String() string

String 字符串.

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 NewSlice

func NewSlice[T constraints.Ordered](elems ...T) Slice[T]

NewSlice 新建切片.

func (*Slice[T]) Add

func (p *Slice[T]) Add(elems ...T)

Add 增加.

func (Slice[T]) All deprecated

func (p Slice[T]) All(elems ...T) bool

All 全部包含.

Deprecated: 使用 All.

func (Slice[T]) Any deprecated

func (p Slice[T]) Any(elems ...T) bool

Any 任意包含.

Deprecated: 使用 Any.

func (*Slice[T]) Clean added in v0.2.13

func (p *Slice[T]) Clean()

Clean 清空.

func (*Slice[T]) Clip deprecated added in v0.1.5

func (p *Slice[T]) Clip()

Clip 删除未使用空间.

Deprecated: 使用 slices.Clip.

func (Slice[T]) Clone deprecated added in v0.1.5

func (p Slice[T]) Clone() Slice[T]

Clone 克隆.

Deprecated: 使用 slices.Clone.

func (Slice[T]) Compare deprecated added in v0.1.5

func (p Slice[T]) Compare(dst Slice[T]) int

Compare 比较.

Deprecated: 使用 slices.Compare.

func (Slice[T]) Contains deprecated added in v0.1.5

func (p Slice[T]) Contains(elem T) bool

Contains 包含.

Deprecated: 使用 slices.Contains.

func (Slice[T]) Count deprecated

func (p Slice[T]) Count(elem T) int

Count 包含元素数量.

Deprecated: 使用 Count.

func (Slice[T]) Counts

func (p Slice[T]) Counts(elems []T) int

Count 包含切片数量. Deprecated: 使用 Counts.

func (*Slice[T]) Del deprecated

func (p *Slice[T]) Del(elems ...T) Slice[T]

Del 删除.

Deprecated: 使用 Del.

func (*Slice[T]) DelAll deprecated

func (p *Slice[T]) DelAll(elems ...T) Slice[T]

DelAll 删除全部.

Deprecated: 使用 DelAll.

func (*Slice[T]) Delete deprecated added in v0.1.5

func (p *Slice[T]) Delete(start, end int)

Delete 删除.

Deprecated: 使用 slices.Delete.

func (Slice[T]) Equal deprecated

func (p Slice[T]) Equal(dst Slice[T]) bool

Equal 比较.

Deprecated: 使用 slices.Equal.

func (*Slice[T]) Grow deprecated added in v0.1.5

func (p *Slice[T]) Grow(size int)

Grow 扩展.

Deprecated: 使用 slices.Grow.

func (Slice[T]) Has deprecated

func (p Slice[T]) Has(elem T) bool

Has 包含.

Deprecated: 使用Contains.

func (Slice[T]) Index deprecated

func (p Slice[T]) Index(elem T) int

Index 位置.

Deprecated: 使用 slices.Index.

func (Slice[T]) Indexs deprecated

func (p Slice[T]) Indexs(elems []T) int

Indexs 包含.

Deprecated: 使用 Index.

func (*Slice[T]) Insert deprecated added in v0.1.5

func (p *Slice[T]) Insert(index int, elems ...T)

Insert 插入.

Deprecated: 使用 slices.Insert.

func (Slice[T]) Join deprecated

func (p Slice[T]) Join(sep string) string

Join 集合连接.

Deprecated: 使用 Join.

func (Slice[T]) Len

func (p Slice[T]) Len() int

Len 长度.

func (Slice[T]) Less

func (p Slice[T]) Less(i, j int) bool

Less 小于.

func (*Slice[T]) Push added in v0.1.5

func (p *Slice[T]) Push(elems ...T)

Push 顶部压入元素.

func (*Slice[T]) Replace deprecated

func (p *Slice[T]) Replace(oldSlice, newSlice []T, num int) Slice[T]

Replace 替换.

Deprecated: 使用 Replace.

func (Slice[T]) ReplaceAll deprecated

func (p Slice[T]) ReplaceAll(oldSlice, newSlice []T) Slice[T]

ReplaceAll 全部替换.

Deprecated: 使用 ReplaceAll.

func (Slice[T]) String

func (p Slice[T]) String() string

String 转换成字符串.

func (Slice[T]) Swap

func (p Slice[T]) Swap(i, j int)

Swap 交换.

func (*Slice[T]) Unique deprecated added in v0.1.4

func (p *Slice[T]) Unique()

Unique 唯一.

Deprecated: 使用 Unique.

Directories

Path Synopsis
Package ordered 排序.
Package ordered 排序.
Package treemap 左倾红黑树 Map.
Package treemap 左倾红黑树 Map.

Jump to

Keyboard shortcuts

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