base

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2022 License: MIT Imports: 6 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
)

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 Panic

func Panic(err error)

Panic 恐慌.

func Panic1

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

Panic1 恐慌1.

func Panic2

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

Panic2 恐慌2.

func Panic3

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

Panic3 恐慌3.

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

func Pass

func Pass(error)

func Pass1

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

func Pass2

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

func Pass3

func Pass3[T1 any, T2 any, T3 any](param1 T1, param2 T2, param3 T3, err error) (T1, T2, T3)

func Round

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

Round 四舍五入.

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 (Map[K, V]) All

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

func (Map[K, V]) Any

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

func (Map[K, V]) Del

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

func (Map[K, V]) DelMap

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

func (Map[K, V]) Has

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

func (Map[K, V]) Keys

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

func (Map[K, V]) Put

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

func (Map[K, V]) String

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

String 字符串.

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) Slice[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]) Count

func (p Slice[T]) Count(elem T) (count 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]) Equal

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

Equal 比较.

func (Slice[T]) Has

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

Has 包含.

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]) 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]) 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() Slice[T]

Unique 唯一.

Jump to

Keyboard shortcuts

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