Documentation ¶
Overview ¶
Package base 基础类型.
包括 Map, Set, Slice, JSNumber.
Index ¶
- Constants
- Variables
- func Append[S ~[]E, E any](max int, slice S, elems ...E) S
- func Bytes2Number[T constraints.Integer | constraints.Float](data []byte) T
- func Conversion[T any](obj any, must bool) T
- 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 FormatFloat[T constraints.Float | constraints.Integer](num T, prec int) string
- func If[T any](condition bool, trueVal, falseVal T) T
- func Index[T comparable](slices, sub []T) int
- func Itoa[T constraints.Integer | constraints.Float](num T) string
- func JSNumber[T constraints.Integer | constraints.Float](num T) T
- func Join[E any](slice []E, sep string) string
- func LevenshteinDistance(str1, str2 string) int
- 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 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(yield func(error))
- func Replace[E comparable](slice, sub, newSub []E, num int) []E
- func ReplaceAll[E comparable](slice, sub, newSub []E) []E
- func Round[I constraints.Integer, F constraints.Float](float F) I
- 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 Wildcard(str string, pattern string) bool
- 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
Examples ¶
Constants ¶
View Source
const ( There = 3 Five = 5 Six = 6 Seven = 7 Nine = 9 Ten = 10 Hundred = 100 Million = 1e6 HundredMillion = 1e8 Billion = 1e9 )
View Source
const ( One = 1 << iota Two Four Eight Sixteen ThirtyTwo SixtyFour OneHundredTwentyEight TwoHundredFiftySix FiveHundredTwelve Kilo )
View Source
const SepInitialisms = rune(-3)
SepInitialisms 首字母缩写分割.
Variables ¶
View Source
var ( ErrConversion = errors.New("conversion bool is false") ErrNotNum = errors.New("not num") )
View Source
var CommonInitialisms = [...]string{
"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
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { fmt.Println(base.Del([]int{1, 2, 3})) fmt.Println(base.Del([]int{}, 3)) fmt.Println(base.Del([]int{1, 2, 3}, 3)) fmt.Println(base.Del([]int{1, 2, 3, 4, 5}, 1, -2, 4)) }
Output: [1 2 3] [] [1 2] [2 3 5]
func DelAll ¶ added in v0.3.22
func DelAll[E comparable](slice []E, elems ...E) []E
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { fmt.Println(base.DelAll([]int{1, 2, 3})) fmt.Println(base.DelAll([]int{}, 3)) fmt.Println(base.DelAll([]int{1, 3, 2, 3}, 3)) }
Output: [1 2 3] [] [1 2]
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 If ¶ added in v0.5.41
If 三元表达式.
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { fmt.Println(base.If(3 > 2, 3, 2)) fmt.Println(base.If(3 < 2, 3, 2)) }
Output: 3 2
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[T constraints.Integer | constraints.Float](num T) T
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 Sorts ¶ added in v0.2.10
func Sorts[T constraints.Ordered, A any](keys []T, slices ...[]A)
Sorts keys 和 slices 都根据keys排序.
Types ¶
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
Source Files ¶
Click to show internal directories.
Click to hide internal directories.