base

package
v1.0.51 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 11 Imported by: 15

Documentation

Overview

Package base 基础类型.

包括 Map, Set, Slice, JSNumber.

Index

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

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

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

func Errors(errs ...error) error

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

func If[T any](condition bool, trueVal, falseVal T) T

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 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[T constraints.Integer | constraints.Float](num T) T

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 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 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(yield 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 Round

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

Round 四舍五入.

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

func Wildcard(str string, pattern string) bool

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

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

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 字符串.

Directories

Path Synopsis
Package treemap 左倾红黑树 Map.
Package treemap 左倾红黑树 Map.

Jump to

Keyboard shortcuts

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