base

package
v0.3.21 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2022 License: MIT Imports: 11 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 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 ErrConversion = errors.New("conversion bool is false")
View Source
var None struct{} // nolint

None 无值.

Functions

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

func Has[E comparable](elems []E, elem E) bool

Has 包含.

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

func Round

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

Round 四舍五入.

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, nums ...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 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]) 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 added in v0.1.5

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

Clear 清除.

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

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

Clone 克隆.

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

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

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

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

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

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

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

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

All 全部包含.

func (Slice[T]) Any

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

Any 任意包含.

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

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

Clean 清空.

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

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

Clip 删除未使用空间.

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

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

Clone 克隆.

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

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

Compare 比较.

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

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

Contains 包含.

func (Slice[T]) Count

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

Count 包含元素数量.

func (Slice[T]) Counts

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

Count 包含切片数量.

func (*Slice[T]) Del

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

Del 删除.

func (*Slice[T]) DelAll

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

DelAll 删除全部.

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

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

Delete 删除.

func (Slice[T]) Equal

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

Equal 比较.

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

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

Grow 扩展.

func (Slice[T]) Has deprecated

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

Has 包含.

Deprecated: 使用Contains.

func (Slice[T]) Index

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

Index 位置.

func (Slice[T]) Indexs

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

Indexs 包含.

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

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

Insert 插入.

func (Slice[T]) Join

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

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

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

Replace 替换.

func (Slice[T]) ReplaceAll

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

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

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

Unique 唯一.

Jump to

Keyboard shortcuts

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