Documentation ¶
Overview ¶
Package base 基础类型.
包括 Map, Set, Slice, JSNumber.
Index ¶
- Constants
- Variables
- func Bytes2Number[T constraints.Integer | constraints.Float](data []byte) T
- func FormatFloat[T constraints.Float | constraints.Integer](num T, prec int) string
- func Itoa[T constraints.Integer | constraints.Float](num T) string
- func JSNumber[Num constraints.Integer | constraints.Float](num Num) Num
- func Number2Bytes[T constraints.Integer | constraints.Float](num T) []byte
- func Panic(err error)
- func Panic1[T any](t T, err error) T
- func Panic2[T1, T2 any](t1 T1, t2 T2, err error) (T1, T2)
- func Panic3[T1, T2, T3 any](t1 T1, t2 T2, t3 T3, err error) (T1, T2, T3)
- func ParseFloat[T constraints.Float](str string) (T, error)
- func ParseInteger[T constraints.Integer](str string) (T, error)
- func Pass(error)
- func Pass1[T any](param T, err error) T
- func Pass2[T1 any, T2 any](param1 T1, param2 T2, err error) (T1, T2)
- func Pass3[T1 any, T2 any, T3 any](param1 T1, param2 T2, param3 T3, err error) (T1, T2, T3)
- func Round[I constraints.Integer, F constraints.Float](float F) I
- type Map
- func (m Map[K, V]) All(keys ...K) bool
- func (m Map[K, V]) Any(keys ...K) bool
- func (m Map[K, V]) Del(keys ...K)
- func (m Map[K, V]) DelMap(elems ...Map[K, V])
- func (m Map[K, V]) Has(key K) bool
- func (m Map[K, V]) Keys() []K
- func (m Map[K, V]) Put(elems ...Map[K, V])
- func (m Map[K, V]) String() string
- 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) Slice[T]
- func (p Slice[T]) All(elems ...T) bool
- func (p Slice[T]) Any(elems ...T) bool
- func (p Slice[T]) Count(elem T) (count int)
- func (p Slice[T]) Counts(elems []T) (count int)
- func (p *Slice[T]) Del(elems ...T) Slice[T]
- func (p *Slice[T]) DelAll(elems ...T) Slice[T]
- func (p Slice[T]) Equal(target Slice[T]) bool
- func (p Slice[T]) Has(elem T) bool
- func (p Slice[T]) Index(elem T) int
- func (p Slice[T]) Indexs(elems []T) int
- func (p Slice[T]) Join(sep string) string
- func (p Slice[T]) Len() int
- func (p Slice[T]) Less(i, j int) bool
- func (p *Slice[T]) Replace(oldSlice, newSlice []T, num int) Slice[T]
- func (p Slice[T]) ReplaceAll(oldSlice, newSlice []T) Slice[T]
- func (p Slice[T]) String() string
- func (p Slice[T]) Swap(i, j int)
- func (p *Slice[T]) Unique() Slice[T]
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 )
Variables ¶
View Source
var None struct{} // nolint
None 无值.
Functions ¶
func Bytes2Number ¶
func Bytes2Number[T constraints.Integer | constraints.Float](data []byte) T
nolint
func FormatFloat ¶
func FormatFloat[T constraints.Float | constraints.Integer](num T, prec int) string
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 Itoa ¶
func Itoa[T constraints.Integer | constraints.Float](num T) string
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 Number2Bytes ¶
func Number2Bytes[T constraints.Integer | constraints.Float](num T) []byte
func ParseFloat ¶
func ParseFloat[T constraints.Float](str string) (T, error)
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { // 字符串转换成浮点数 fmt.Println(base.Panic1(base.ParseFloat[float32]("3.14"))) }
Output: 3.14
func ParseInteger ¶
func ParseInteger[T constraints.Integer](str string) (T, error)
nolint
Example ¶
package main import ( "fmt" "github.com/xuender/oils/base" ) func main() { // 字符串转换成整数 fmt.Println(base.Panic1(base.ParseInteger[int]("3"))) }
Output: 3
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]
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 ¶
ReplaceAll 全部替换.
Click to show internal directories.
Click to hide internal directories.