Documentation ¶
Overview ¶
Package types supplies some assistant functions about type, such as the type validation and conversion, etc.
Index ¶
- Constants
- Variables
- func Convert(k Kind, v interface{}) (interface{}, error)
- func IsZero(v interface{}) bool
- func MustToBool(v interface{}) bool
- func MustToFloat32(v interface{}) float32
- func MustToFloat64(v interface{}) float64
- func MustToInt(v interface{}) int
- func MustToInt16(v interface{}) int16
- func MustToInt32(v interface{}) int32
- func MustToInt64(v interface{}) int64
- func MustToInt8(v interface{}) int8
- func MustToMapKeys(v interface{}) []string
- func MustToMapValues(v interface{}) []interface{}
- func MustToSlice(v interface{}) []interface{}
- func MustToString(v interface{}) string
- func MustToStringMap(v interface{}) map[string]interface{}
- func MustToTime(v interface{}, layout ...string) time.Time
- func MustToUint(v interface{}) uint
- func MustToUint16(v interface{}) uint16
- func MustToUint32(v interface{}) uint32
- func MustToUint64(v interface{}) uint64
- func MustToUint8(v interface{}) uint8
- func NameToType(name string) string
- func RegisterConverter(k Kind, converter func(interface{}) (interface{}, error))
- func RegisterVerifyFunc(t string, f VerifyFunc) error
- func StringToTime(s string, layout ...string) (t time.Time, err error)
- func ToBool(value interface{}) (bool, error)
- func ToBoolSlice(value interface{}) ([]bool, error)
- func ToDuration(value interface{}) (time.Duration, error)
- func ToDurationSlice(value interface{}) ([]time.Duration, error)
- func ToFloat32(value interface{}) (float32, error)
- func ToFloat64(value interface{}) (float64, error)
- func ToFloat64Slice(value interface{}) ([]float64, error)
- func ToInt(value interface{}) (int, error)
- func ToInt16(value interface{}) (int16, error)
- func ToInt32(value interface{}) (int32, error)
- func ToInt64(value interface{}) (int64, error)
- func ToInt8(value interface{}) (int8, error)
- func ToIntSlice(value interface{}) ([]int, error)
- func ToMapKeys(v interface{}) ([]string, error)
- func ToMapValues(v interface{}) ([]interface{}, error)
- func ToSlice(value interface{}) ([]interface{}, error)
- func ToString(value interface{}) (string, error)
- func ToStringMap(value interface{}) (m map[string]interface{}, err error)
- func ToStringMapBool(value interface{}) (m map[string]bool, err error)
- func ToStringMapString(value interface{}) (m map[string]string, err error)
- func ToStringSlice(value interface{}) ([]string, error)
- func ToTime(value interface{}, layout ...string) (time.Time, error)
- func ToUint(value interface{}) (uint, error)
- func ToUint16(value interface{}) (uint16, error)
- func ToUint32(value interface{}) (uint32, error)
- func ToUint64(value interface{}) (uint64, error)
- func ToUint8(value interface{}) (uint8, error)
- func ToUintSlice(value interface{}) ([]uint, error)
- func VerifyMapValueType(m interface{}, k, t string) (ok bool)
- func VerifySliceValueType(slice interface{}, i int, t string) (ok bool)
- func VerifyType(v interface{}, t string) bool
- type Converter
- type Deque
- type Kind
- type SMap
- func (m SMap) Get(key string, _default interface{}) interface{}
- func (m SMap) GetBool(key string, _default bool) bool
- func (m SMap) GetDuration(key string, _default time.Duration) time.Duration
- func (m SMap) GetFloat32(key string, _default float32) float32
- func (m SMap) GetFloat64(key string, _default float64) float64
- func (m SMap) GetFloat64Slice(key string, _default []float64) []float64
- func (m SMap) GetInt(key string, _default int) int
- func (m SMap) GetInt16(key string, _default int16) int16
- func (m SMap) GetInt32(key string, _default int32) int32
- func (m SMap) GetInt64(key string, _default int64) int64
- func (m SMap) GetInt8(key string, _default int8) int8
- func (m SMap) GetIntSlice(key string, _default []int) []int
- func (m SMap) GetInterfaceSlice(key string, _default []interface{}) []interface{}
- func (m SMap) GetString(key string, _default string) string
- func (m SMap) GetStringMap(key string, _default SMap) SMap
- func (m SMap) GetStringSlice(key string, _default []string) []string
- func (m SMap) GetTime(key string, _default time.Time) time.Time
- func (m SMap) GetUint(key string, _default uint) uint
- func (m SMap) GetUint16(key string, _default uint16) uint16
- func (m SMap) GetUint32(key string, _default uint32) uint32
- func (m SMap) GetUint64(key string, _default uint64) uint64
- func (m SMap) GetUint8(key string, _default uint8) uint8
- func (m SMap) GetUintSlice(key string, _default []uint) []uint
- func (m SMap) Must(key string) interface{}
- func (m SMap) MustBool(key string) bool
- func (m SMap) MustDuration(key string) time.Duration
- func (m SMap) MustFloat32(key string) float32
- func (m SMap) MustFloat64(key string) float64
- func (m SMap) MustFloat64Slice(key string) []float64
- func (m SMap) MustInt(key string) int
- func (m SMap) MustInt16(key string) int16
- func (m SMap) MustInt32(key string) int32
- func (m SMap) MustInt64(key string) int64
- func (m SMap) MustInt8(key string) int8
- func (m SMap) MustIntSlice(key string) []int
- func (m SMap) MustInterfaceSlice(key string) []interface{}
- func (m SMap) MustString(key string) string
- func (m SMap) MustStringMap(key string) SMap
- func (m SMap) MustStringSlice(key string) []string
- func (m SMap) MustTime(key string) time.Time
- func (m SMap) MustUint(key string) uint
- func (m SMap) MustUint16(key string) uint16
- func (m SMap) MustUint32(key string) uint32
- func (m SMap) MustUint64(key string) uint64
- func (m SMap) MustUint8(key string) uint8
- func (m SMap) MustUintSlice(key string) []uint
- func (m SMap) ToMap() map[string]interface{}
- type Set
- func (s Set) Add(elements ...interface{})
- func (s Set) AddInts(elements ...int)
- func (s Set) AddSlices(slices ...interface{})
- func (s Set) AddStrings(elements ...string)
- func (s Set) Clear()
- func (s Set) Copy() Set
- func (s Set) Difference(others ...Set) Set
- func (s Set) DifferenceUpdate(others ...Set)
- func (s Set) Equal(other Set) bool
- func (s Set) ForEach(f func(element interface{}))
- func (s Set) Has(element interface{}) bool
- func (s Set) Intersection(others ...Set) Set
- func (s Set) IntersectionUpdate(others ...Set)
- func (s Set) List() []interface{}
- func (s Set) Pop() interface{}
- func (s Set) Remove(elements ...interface{})
- func (s Set) RemoveInts(elements ...int)
- func (s Set) RemoveStrings(elements ...string)
- func (s Set) Size() int
- func (s Set) String() string
- func (s Set) SymmetricDifference(other Set) Set
- func (s Set) SymmetricDifferenceUpdate(other Set)
- func (s Set) Union(others ...Set) Set
- func (s Set) UnionUpdate(others ...Set)
- func (s Set) Walk(f func(interface{}))
- type VerifyFunc
Examples ¶
Constants ¶
const DateTimeLayout = "2006-01-02 15:04:05"
DateTimeLayout is the DateTime layout to parse the value to the time.Time.
Variables ¶
var ( // ErrNotSliceOrArray is returned when the value is not a slice. ErrNotSliceOrArray = fmt.Errorf("the value is not a slice or array") // ErrNotMap is returned when the value is not a map. ErrNotMap = fmt.Errorf("the value is not a map") // ErrNotString is returned when the type of the key is not string. ErrNotString = fmt.Errorf("the type of the key is not string") // ErrKindNotExist is returned when a certain kind does not exist. ErrKindNotExist = fmt.Errorf("no kind") // ErrUnknownType is returned when not to identify a data type. ErrUnknownType = fmt.Errorf("unknown type") )
var StringSeparator = ""
StringSeparator is the separator of the string slice to split the string.
Functions ¶
func Convert ¶
Convert calls the converter of the kind k to convert the value v.
If the converter of the kind k does not exists, it returns ErrKindNotExist.
func IsZero ¶
func IsZero(v interface{}) bool
IsZero judges whether a value is ZERO.
For "", 0, 0.0, false, 0+0i, nil, and the slice, array or map that the length is 0, they are all ZERO. Others is not ZERO.
Example ¶
// bool fmt.Println("bool:", IsZero(true), IsZero(false)) // int, int8, int16, int32, int64, rune fmt.Println("int:", IsZero(int(1)), IsZero(int(0))) // uint, uint8, uint16, uint32, uint64, byte fmt.Println("uint:", IsZero(uint(1)), IsZero(uint(0))) // complex64, complex128 fmt.Println("complex:", IsZero(complex64(1.1+1.1i)), IsZero(complex64(0.0+0.0i))) // chan, func, map, slice var c chan string var f func() var m map[string]string var s []string fmt.Println("chan func map slice:", IsZero(c), IsZero(f), IsZero(m), IsZero(s)) // ptr ii := 11 fmt.Println("ptr:", IsZero(&ii), IsZero((*int)(nil))) // array fmt.Println("array:", IsZero([3]int{1, 2, 3}), IsZero([0]int{})) // string fmt.Println("string:", IsZero("123"), IsZero("")) // struct type S struct{} fmt.Println("struct:", IsZero(S{})) // interface fmt.Println("interface:", IsZero(interface{}(nil)))
Output: bool: false true int: false true uint: false true complex: false true chan func map slice: true true true true ptr: false true array: false true string: false true struct: false interface: true
func MustToBool ¶
func MustToBool(v interface{}) bool
MustToBool is equal to ToBool, but panic if there is an error.
func MustToFloat32 ¶
func MustToFloat32(v interface{}) float32
MustToFloat32 is equal to ToFloat32, but panic if there is an error.
func MustToFloat64 ¶
func MustToFloat64(v interface{}) float64
MustToFloat64 is equal to ToFloat64, but panic if there is an error.
func MustToInt ¶
func MustToInt(v interface{}) int
MustToInt is equal to ToInt, but panic if there is an error.
func MustToInt16 ¶
func MustToInt16(v interface{}) int16
MustToInt16 is equal to ToInt16, but panic if there is an error.
func MustToInt32 ¶
func MustToInt32(v interface{}) int32
MustToInt32 is equal to ToInt32, but panic if there is an error.
func MustToInt64 ¶
func MustToInt64(v interface{}) int64
MustToInt64 is equal to ToInt64, but panic if there is an error.
func MustToInt8 ¶
func MustToInt8(v interface{}) int8
MustToInt8 is equal to ToInt8, but panic if there is an error.
func MustToMapKeys ¶
func MustToMapKeys(v interface{}) []string
MustToMapKeys is equal to ToMapKeys, but panic if there is an error.
func MustToMapValues ¶
func MustToMapValues(v interface{}) []interface{}
MustToMapValues is equal to ToMapValues, but panic if there is an error.
func MustToSlice ¶
func MustToSlice(v interface{}) []interface{}
MustToSlice is equal to ToSlice, but panic if there is an error.
func MustToString ¶
func MustToString(v interface{}) string
MustToString is equal to ToString, but panic if there is an error.
func MustToStringMap ¶
func MustToStringMap(v interface{}) map[string]interface{}
MustToStringMap is equal to ToStringMap, but panic if there is an error.
func MustToTime ¶
MustToTime is equal to ToTime, but panic if there is an error.
func MustToUint ¶
func MustToUint(v interface{}) uint
MustToUint is equal to ToUint, but panic if there is an error.
func MustToUint16 ¶
func MustToUint16(v interface{}) uint16
MustToUint16 is equal to ToUint16, but panic if there is an error.
func MustToUint32 ¶
func MustToUint32(v interface{}) uint32
MustToUint32 is equal to ToUint32, but panic if there is an error.
func MustToUint64 ¶
func MustToUint64(v interface{}) uint64
MustToUint64 is equal to ToUint64, but panic if there is an error.
func MustToUint8 ¶
func MustToUint8(v interface{}) uint8
MustToUint8 is equal to ToUint8, but panic if there is an error.
func NameToType ¶
NameToType returns the type string by the name.
The name is the name used by VerifyType, such as string for string, strings for []string, or string2string for map[string]string.
Example ¶
fmt.Println(NameToType("string")) fmt.Println(NameToType("strings")) fmt.Println(NameToType("string2string"))
Output: string []string map[string]string
func RegisterConverter ¶
RegisterConverter registers a converter of the kind k.
By default it has registered the kinds as follow:
Bool String Float64 Int, Int32, Int64 Uint, Uint32, Uint64 Time, RFC3339Time
func RegisterVerifyFunc ¶
func RegisterVerifyFunc(t string, f VerifyFunc) error
RegisterVerifyFunc registers a type verification function.
func StringToTime ¶
StringToTime does the best to parse a string into a time.Time.
If giving layout, it will use it, or attempt to guess it by using a predefined list of formats.
func ToBool ¶
ToBool does the best to convert any certain value to bool.
For the string, the true value is
"t", "T", "1", "on", "On", "ON", "true", "True", "TRUE", "yes", "Yes", "YES"
the false value is
"f", "F", "0", "off", "Off", "OFF", "false", "False", "FALSE", "no", "No", "NO", ""
For other types, if the value is ZERO of the type, it's false. Or it's true.
func ToBoolSlice ¶
ToBoolSlice casts an interface to a []bool type.
func ToDuration ¶
ToDuration casts an interface to a time.Duration type.
func ToDurationSlice ¶
ToDurationSlice casts an interface to a []time.Duration type.
func ToFloat64Slice ¶
ToFloat64Slice casts an interface to a []float64 type.
func ToIntSlice ¶
ToIntSlice casts an interface to a []int type.
func ToMapKeys ¶
ToMapKeys returns all the keys of a map.
If the value is not a map or the key is not string, it returns an error. But if the value is nil, it will return a empty slice, not an error instead.
If you ensure that v is a map, and its key is the string type, you can ignore the error.
For map[string]interface{}, map[string]string and map[string]int, they have already been optimized.
func ToMapValues ¶
func ToMapValues(v interface{}) ([]interface{}, error)
ToMapValues returns all the values of a map.
If the value is not a map, it returns an error. But if the value is nil, it will return a empty slice, not an error instead.
If you ensure that v is a map, you can ignore the error.
For map[string]interface{}, map[string]string and map[string]int, they have already been optimized.
func ToSlice ¶
func ToSlice(value interface{}) ([]interface{}, error)
ToSlice converts any slice type of []interface{}.
func ToString ¶
ToString does the best to convert any certain value to string.
For time.Time, it will use time.RFC3339Nano to format it.
func ToStringMap ¶
ToStringMap casts an interface to a map[string]interface{} type.
func ToStringMapBool ¶
ToStringMapBool casts an interface to a map[string]bool type.
func ToStringMapString ¶
ToStringMapString casts an interface to a map[string]string type.
func ToStringSlice ¶
ToStringSlice casts an interface to a []string type.
If value is string and the global variable StringSeparator is not "", the value will be split into []string by the string separator.
func ToTime ¶
ToTime does the best to convert any certain value to time.Time.
If value is string or []byte, it will use StringToTime to convert it.
func ToUintSlice ¶
ToUintSlice casts an interface to a []uint type.
func VerifyMapValueType ¶
VerifyMapValueType verifies whether the type of the value of the key in the map is the given type.
Return false if m is not the map type or the map does not have the key.
Notice: the type of the key of the map type must be string, or return false.
Example ¶
v1 := map[string]interface{}{ "a": 11, "b": "22", } v2 := map[int]interface{}{ 1: "a", 2: "b", } fmt.Println(VerifyMapValueType(v1, "a", "int")) fmt.Println(VerifyMapValueType(v1, "b", "string")) fmt.Println(VerifyMapValueType(v1, "b", "int")) fmt.Println(VerifyMapValueType(v1, "c", "string")) fmt.Println(VerifyMapValueType(v2, "1", "string"))
Output: true true false false false
func VerifySliceValueType ¶
VerifySliceValueType verifies whether the type of the ith value of the slice is the given type.
Return false if the ith value of slice does not exist, that's, i>=len(slice). Return false if slice is not a slice or array type, too.
func VerifyType ¶
VerifyType verifies whether the type of v is t.
The supported types are below:
t(string) Go Type / Function Call ------------------------------------------- "zero" IsZero(v) "nil" nil "bool" bool "string" string "byte" byte "rune" rune "int" int "int8" int8 "int16" int16 "int32" int32 "int64" int64 "uint" uint "uint8" uint8 "uint16" uint16 "uint32" uint32 "uint64" uint64 "float32" float32 "float64" float64 "complex64" complex64 "complex128" complex128 "bools" []bool "strings" []string "bytes" []byte "runes" []rune "ints" []int "int8s" []int8 "int16s" []int16 "int32s" []int32 "int64s" []int64 "uints" []uint "uint8s" []uint8 "uint16s" []uint16 "uint32s" []uint32 "uint64s" []uint64 "float32s" []float32 "float64s" []float64 "complex64s" []complex64 "complex128s" []complex128 "string2string" map[string]string, "string2interface" map[string]interface{} "int642interface" map[int64]interface{} "int642string" map[int64]string "int2string" map[int]string "int2interface" map[int]interface{} "uint642interface" map[uint64]interface{} "uint642string" map[uint64]string "uint2string" map[uint]string "uint2interface" map[uint]interface{}
Notice: You can add the new type verification by RegisterVerifyFunc it.
Example ¶
fmt.Println(VerifyType(int64(1), "int64")) fmt.Println(VerifyType(int64(1), "int32")) fmt.Println(VerifyType(byte(1), "uint8")) fmt.Println(VerifyType(byte(1), "int8")) fmt.Println(VerifyType("123", "string")) fmt.Println(VerifyType("123", "bytes")) fmt.Println(VerifyType([]byte("123"), "bytes")) fmt.Println(VerifyType([]byte("123"), "string")) fmt.Println(VerifyType(map[string]string{"123": "abc"}, "string2string")) fmt.Println(VerifyType(map[string]string{"123": "abc"}, "string2interface"))
Output: true false true false true false true false true false
Types ¶
type Converter ¶
type Converter struct {
// contains filtered or unexported fields
}
Converter is used to convert the value by the Scan method. So you can use it as the argument of Rows.Scan() in sql.
func NewConverter ¶
NewConverter returns a Converter to convert a value to the type kind.
type Deque ¶
type Deque struct {
// contains filtered or unexported fields
}
Deque implements an efficient double-ended queue.
Internally it is composed of a doubly-linked list (list.List) of blocks. Each block is a slice that holds 0 to blockLen items. The Deque starts with one block. Blocks are added to the front and back when the edge blocks fill up as items are pushed onto the deque. Edge blocks are removed when blocks become empty as items are popped off the Deque.
Only the front and back blocks may contain less than blockLen items. The first element in the Deque is d.blocks.Front().Value[d.frontIdx]. The last element in the Deque is d.blocks.Back().Value[d.backIdx].
This approach is more efficient than using a standard doubly-linked list for a queue because memory allocation is only required every blockLen items, instead of for each pushed item. Conversely, fewer memory deallocations are required when popping items. Bookkeeping overhead per item is also reduced.
Usage:
d := deque.New() d.PushFront("foo") d.PushBack("bar") d.PushBack("123") l := d.Len() // l == 3 v, ok := d.PopFront() // v.(string) == "foo", ok == true v, ok = d.PopFront() // v.(string) == "bar", ok == true v, ok = d.PopBack() // v.(string) == "123", ok == true v, ok = d.PopBack() // v == nil, ok == false v, ok = d.PopFront() // v == nil, ok == false l = d.Len() // l == 0
Example ¶
de := NewDeque() de.PushBack(1) de.PushBack(2) de.PushBack(3) de.PushFront("a") de.PushFront("b") de.PushFront("c") de.Each(func(v interface{}) { fmt.Println(v) }) fmt.Println(de.PopBack()) fmt.Println(de.PopFront())
Output: c b a 1 2 3 3 true c true
func NewDequeWithMaxLen ¶
NewDequeWithMaxLen returns a new Deque instance which is limited to a certain length. Pushes which cause the length to exceed the specified size will cause an item to be dropped from the opposing side.
A maxLen of 0 means that there is no maximum length limit in place.
func (*Deque) Each ¶
func (d *Deque) Each(f func(v interface{}))
Each will traverse each element then pass f.
func (*Deque) PopBack ¶
PopBack removes an item from the back of the queue and returns it. The returned flag is true unless there were no items left in the queue.
func (*Deque) PopFront ¶
PopFront removes an item from the front of the queue and returns it. The returned flag is true unless there were no items left in the queue.
type SMap ¶ added in v6.1.0
type SMap map[string]interface{}
SMap is the proxy of map[string]interface{}.
func (SMap) Get ¶ added in v6.1.0
Get returns the value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not string.
func (SMap) GetBool ¶ added in v6.1.0
GetBool returns the bool value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not bool.
func (SMap) GetDuration ¶ added in v6.1.0
GetDuration returns the time.Duration value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not time.Duration.
func (SMap) GetFloat32 ¶ added in v6.1.0
GetFloat32 returns the float32 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not float32.
func (SMap) GetFloat64 ¶ added in v6.1.0
GetFloat64 returns the float64 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not float64.
func (SMap) GetFloat64Slice ¶ added in v6.1.0
GetFloat64Slice returns the []float64 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not []float64.
func (SMap) GetInt ¶ added in v6.1.0
GetInt returns the int value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not int.
func (SMap) GetInt16 ¶ added in v6.1.0
GetInt16 returns the int16 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not int16.
func (SMap) GetInt32 ¶ added in v6.1.0
GetInt32 returns the int32 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not int32.
func (SMap) GetInt64 ¶ added in v6.1.0
GetInt64 returns the int64 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not int64.
func (SMap) GetInt8 ¶ added in v6.1.0
GetInt8 returns the int8 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not int8.
func (SMap) GetIntSlice ¶ added in v6.1.0
GetIntSlice returns the []int value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not []int.
func (SMap) GetInterfaceSlice ¶ added in v6.1.0
GetInterfaceSlice returns the []interface{} value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not []interface{}.
func (SMap) GetString ¶ added in v6.1.0
GetString returns the string value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not string.
func (SMap) GetStringMap ¶ added in v6.1.0
GetStringMap returns the SMap value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not SMap or map[string]interface{}.
func (SMap) GetStringSlice ¶ added in v6.1.0
GetStringSlice returns the []string value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not []string.
func (SMap) GetTime ¶ added in v6.1.0
GetTime returns the time.Time value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not time.Time.
func (SMap) GetUint ¶ added in v6.1.0
GetUint returns the uint value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not uint.
func (SMap) GetUint16 ¶ added in v6.1.0
GetUint16 returns the uint16 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not uint16.
func (SMap) GetUint32 ¶ added in v6.1.0
GetUint32 returns the uint32 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not uint32.
func (SMap) GetUint64 ¶ added in v6.1.0
GetUint64 returns the uint64 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not uint64.
func (SMap) GetUint8 ¶ added in v6.1.0
GetUint8 returns the uint8 value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not uint8.
func (SMap) GetUintSlice ¶ added in v6.1.0
GetUintSlice returns the []uint value by the key, which will return the default instead if no the key and panic if the type of the vlaue is not []uint.
func (SMap) MustDuration ¶ added in v6.1.0
MustDuration is the same as GetDuration, but panic if no the key.
func (SMap) MustFloat32 ¶ added in v6.1.0
MustFloat32 is the same as GetFloat32, but panic if no the key.
func (SMap) MustFloat64 ¶ added in v6.1.0
MustFloat64 is the same as GetFloat64, but panic if no the key.
func (SMap) MustFloat64Slice ¶ added in v6.1.0
MustFloat64Slice is the same as GetFloat64Slice, but panic if no the key.
func (SMap) MustIntSlice ¶ added in v6.1.0
MustIntSlice is the same as GetIntSlice, but panic if no the key.
func (SMap) MustInterfaceSlice ¶ added in v6.1.0
MustInterfaceSlice is the same as GetInterfaceSlice, but panic if no the key.
func (SMap) MustString ¶ added in v6.1.0
MustString is the same as GetString, but panic if no the key.
func (SMap) MustStringMap ¶ added in v6.1.0
MustStringMap is the same as GetStringMap, but panic if no the key.
func (SMap) MustStringSlice ¶ added in v6.1.0
MustStringSlice is the same as GetStringSlice, but panic if no the key.
func (SMap) MustUint16 ¶ added in v6.1.0
MustUint16 is the same as GetUint16, but panic if no the key.
func (SMap) MustUint32 ¶ added in v6.1.0
MustUint32 is the same as GetUint32, but panic if no the key.
func (SMap) MustUint64 ¶ added in v6.1.0
MustUint64 is the same as GetUint64, but panic if no the key.
func (SMap) MustUintSlice ¶ added in v6.1.0
MustUintSlice is the same as GetUintSlice, but panic if no the key.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is a set type.
The element of the set must be hashable, such as int, string, array, etc. Notice: slice and map is not hashable.
The set supports the mixed types, but suggest to use the consistent type in a set.
Example ¶
s1 := NewSet(1, 2, 3) s2 := NewSet("a", "b", "c") s1.Add(3, 4, 5) s2.Add("c", "d", "e") fmt.Println(s1.Size(), s1.Size()) fmt.Println(s1.Has(4), s1.Has(5), s1.Has(6), s2.Has("d"), s2.Has("e"), s2.Has("z")) // Output: // 5 5 // true true false true true false s1.RemoveInts(1, 2, 9) s2.RemoveStrings("a", "b", "z") fmt.Println(s1.Size(), s2.Size()) fmt.Println(s1.Has(1), s1.Has(2), s2.Has("a"), s2.Has("b")) // Output: // 3 3 // false false false false list1 := s1.List() fmt.Println(list1[0]) fmt.Println(list1[1]) fmt.Println(list1[2]) // Unordered output: // 3 // 4 // 5 list2 := s2.List() fmt.Println(list2[0]) fmt.Println(list2[1]) fmt.Println(list2[2]) // Unordered output: // c // d // e union := s1.Union(s2) diff := s1.Difference(s2) inter := s1.Intersection(s2) sdiff := s1.SymmetricDifference(s2) fmt.Println(union.Size(), diff.Size(), inter.Size(), sdiff.Size()) // Output: // 6 3 0 6 fmt.Println(union.Difference(s1).Equal(s2)) fmt.Println(union.Equal(sdiff)) // Output: // true // true list := union.List() fmt.Println(list[0]) fmt.Println(list[1]) fmt.Println(list[2]) fmt.Println(list[3]) fmt.Println(list[4]) fmt.Println(list[5]) // Unordered output: // 3 // 4 // 5 // c // d // e set := NewSet([2]int{1, 2}, [2]int{3, 4}) fmt.Println(set.Has([2]int{1, 2})) fmt.Println(set.Has([2]int{2, 3})) fmt.Println(set.Has([3]int{2, 3, 4})) fmt.Println(set.Has([2]string{"a", "b"})) // true // false // false // false
Output: 5 5 true true false true true false 3 3 false false false false 3 4 5 c d e 6 3 0 6 true true 3 4 5 c d e true false false false
func NewSet ¶
func NewSet(elements ...interface{}) Set
NewSet returns a new Set.
If the element is string, it will ignore the empty string.
func NewSetFromSlices ¶ added in v6.1.0
func NewSetFromSlices(slices ...interface{}) Set
NewSetFromSlices returns a new Set, which initializes the set from the other slices.
Notice: it will panic if the arguments is not the slice or array.
func (Set) Add ¶
func (s Set) Add(elements ...interface{})
Add adds some elements into the set.
If the element is string, it will ignore the empty string.
func (Set) AddSlices ¶ added in v6.1.0
func (s Set) AddSlices(slices ...interface{})
AddSlices adds the elements of all the slices into the set.
Notice: if the argument is not a slice or array, it will add it by using the method Add() instead.
Example ¶
set := NewSetFromSlices([]string{"a", "b"}, []string{"c", "d"}) set.AddSlices([]string{"o", "p", "q"}, []string{"r", "s", "t"}) set.AddSlices([]string{"x", "y", "z"}) ss := make([]string, 0, set.Size()) set.Walk(func(v interface{}) { ss = append(ss, v.(string)) }) sort.Strings(ss) fmt.Println(set.Size()) fmt.Println(len(ss)) fmt.Println(ss)
Output: 13 13 [a b c d o p q r s t x y z]
func (Set) AddStrings ¶
AddStrings adds some string elements into the set.
It will ignore the empty string.
func (Set) Difference ¶
Difference returns a new set with elements in the set that are not in the others.
func (Set) DifferenceUpdate ¶
DifferenceUpdate updates the set, removing the elements found in others.
func (Set) ForEach ¶ added in v6.1.0
func (s Set) ForEach(f func(element interface{}))
ForEach travels the elements of the set instead of the deprecated Walk.
func (Set) Intersection ¶
Intersection returns a new set with elements common to the set and all others.
func (Set) IntersectionUpdate ¶
IntersectionUpdate updates the set, keeping only elements found in it and all others.
func (Set) Pop ¶
func (s Set) Pop() interface{}
Pop removes and returns an arbitrary element from the set. But return nil if the set is empty.
func (Set) Remove ¶
func (s Set) Remove(elements ...interface{})
Remove removes the elements from the set.
func (Set) RemoveInts ¶
RemoveInts removes the int elements from the set.
func (Set) RemoveStrings ¶
RemoveStrings removes the string elements from the set.
func (Set) SymmetricDifference ¶
SymmetricDifference returns a new set with elements in either the set or other but not both.
func (Set) SymmetricDifferenceUpdate ¶
SymmetricDifferenceUpdate updates the set, keeping only elements found in either set, but not in both.
func (Set) UnionUpdate ¶
UnionUpdate updates the set, adding the elements from all others.
type VerifyFunc ¶
VerifyFunc is a function to verifty whether the type of a value is the given type, the first argument of which is the value, and the second of which is the given type.