types

package
v6.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: Apache-2.0 Imports: 9 Imported by: 2

Documentation

Overview

Package types supplies some assistant functions about type, such as the type validation and conversion, etc.

Index

Examples

Constants

View Source
const DateTimeLayout = "2006-01-02 15:04:05"

DateTimeLayout is the DateTime layout to parse the value to the time.Time.

Variables

View Source
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")
)
View Source
var StringSeparator = ""

StringSeparator is the separator of the string slice to split the string.

Functions

func Convert

func Convert(k Kind, v interface{}) (interface{}, error)

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

func MustToTime(v interface{}, layout ...string) time.Time

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

func NameToType(name string) string

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

func RegisterConverter(k Kind, converter func(interface{}) (interface{}, error))

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

func StringToTime(s string, layout ...string) (t time.Time, err error)

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

func ToBool(value interface{}) (bool, error)

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

func ToBoolSlice(value interface{}) ([]bool, error)

ToBoolSlice casts an interface to a []bool type.

func ToDuration

func ToDuration(value interface{}) (time.Duration, error)

ToDuration casts an interface to a time.Duration type.

func ToDurationSlice

func ToDurationSlice(value interface{}) ([]time.Duration, error)

ToDurationSlice casts an interface to a []time.Duration type.

func ToFloat32

func ToFloat32(value interface{}) (float32, error)

ToFloat32 does the best to convert any certain value to float32.

func ToFloat64

func ToFloat64(value interface{}) (float64, error)

ToFloat64 does the best to convert any certain value to float64.

func ToFloat64Slice

func ToFloat64Slice(value interface{}) ([]float64, error)

ToFloat64Slice casts an interface to a []float64 type.

func ToInt

func ToInt(value interface{}) (int, error)

ToInt does the best to convert any certain value to int.

func ToInt16

func ToInt16(value interface{}) (int16, error)

ToInt16 does the best to convert any certain value to int16.

func ToInt32

func ToInt32(value interface{}) (int32, error)

ToInt32 does the best to convert any certain value to int32.

func ToInt64

func ToInt64(value interface{}) (int64, error)

ToInt64 does the best to convert any certain value to int64.

func ToInt8

func ToInt8(value interface{}) (int8, error)

ToInt8 does the best to convert any certain value to int8.

func ToIntSlice

func ToIntSlice(value interface{}) ([]int, error)

ToIntSlice casts an interface to a []int type.

func ToMapKeys

func ToMapKeys(v interface{}) ([]string, error)

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

func ToString(value interface{}) (string, error)

ToString does the best to convert any certain value to string.

For time.Time, it will use time.RFC3339Nano to format it.

func ToStringMap

func ToStringMap(value interface{}) (m map[string]interface{}, err error)

ToStringMap casts an interface to a map[string]interface{} type.

func ToStringMapBool

func ToStringMapBool(value interface{}) (m map[string]bool, err error)

ToStringMapBool casts an interface to a map[string]bool type.

func ToStringMapString

func ToStringMapString(value interface{}) (m map[string]string, err error)

ToStringMapString casts an interface to a map[string]string type.

func ToStringSlice

func ToStringSlice(value interface{}) ([]string, error)

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

func ToTime(value interface{}, layout ...string) (time.Time, error)

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 ToUint

func ToUint(value interface{}) (uint, error)

ToUint does the best to convert any certain value to uint.

func ToUint16

func ToUint16(value interface{}) (uint16, error)

ToUint16 does the best to convert any certain value to uint16.

func ToUint32

func ToUint32(value interface{}) (uint32, error)

ToUint32 does the best to convert any certain value to uint32.

func ToUint64

func ToUint64(value interface{}) (uint64, error)

ToUint64 does the best to convert any certain value to uint64.

func ToUint8

func ToUint8(value interface{}) (uint8, error)

ToUint8 does the best to convert any certain value to uint8.

func ToUintSlice

func ToUintSlice(value interface{}) ([]uint, error)

ToUintSlice casts an interface to a []uint type.

func VerifyMapValueType

func VerifyMapValueType(m interface{}, k, t string) (ok bool)

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

func VerifySliceValueType(slice interface{}, i int, t string) (ok bool)

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

func VerifyType(v interface{}, t string) bool

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

func NewConverter(kind Kind) Converter

NewConverter returns a Converter to convert a value to the type kind.

func (*Converter) Scan

func (c *Converter) Scan(src interface{}) error

Scan converts the value src.

func (Converter) Value

func (c Converter) Value() interface{}

Value returns the inner converted result.

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 NewDeque

func NewDeque() *Deque

NewDeque returns a new Deque instance.

func NewDequeWithMaxLen

func NewDequeWithMaxLen(maxLen int) *Deque

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) Len

func (d *Deque) Len() int

Len returns the number of items stored in the queue.

func (*Deque) PopBack

func (d *Deque) PopBack() (interface{}, bool)

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

func (d *Deque) PopFront() (interface{}, bool)

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.

func (*Deque) PushBack

func (d *Deque) PushBack(item interface{})

PushBack adds an item to the back of the queue.

func (*Deque) PushFront

func (d *Deque) PushFront(item interface{})

PushFront adds an item to the front of the queue.

type Kind

type Kind int

Kind represents the kind of the converter.

const (
	Unknown Kind = iota
	Nil
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Float32
	Float64
	String
	Bytes
	Time        // For the format "YYYY-MM-DD HH:MM:SS"
	RFC3339Time // For the format time.RFC3339
)

Predefine some kinds.

type SMap added in v6.1.0

type SMap map[string]interface{}

SMap is the proxy of map[string]interface{}.

func NewSMap added in v6.1.0

func NewSMap(capacity int) SMap

NewSMap returns a new SMap with the capacity.

func (SMap) Get added in v6.1.0

func (m SMap) Get(key string, _default interface{}) interface{}

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

func (m SMap) GetBool(key string, _default bool) bool

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

func (m SMap) GetDuration(key string, _default time.Duration) time.Duration

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

func (m SMap) GetFloat32(key string, _default float32) float32

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

func (m SMap) GetFloat64(key string, _default float64) float64

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

func (m SMap) GetFloat64Slice(key string, _default []float64) []float64

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

func (m SMap) GetInt(key string, _default int) int

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

func (m SMap) GetInt16(key string, _default int16) int16

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

func (m SMap) GetInt32(key string, _default int32) int32

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

func (m SMap) GetInt64(key string, _default int64) int64

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

func (m SMap) GetInt8(key string, _default int8) int8

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

func (m SMap) GetIntSlice(key string, _default []int) []int

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

func (m SMap) GetInterfaceSlice(key string, _default []interface{}) []interface{}

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

func (m SMap) GetString(key string, _default string) string

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

func (m SMap) GetStringMap(key string, _default SMap) SMap

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

func (m SMap) GetStringSlice(key string, _default []string) []string

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

func (m SMap) GetTime(key string, _default time.Time) time.Time

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

func (m SMap) GetUint(key string, _default uint) uint

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

func (m SMap) GetUint16(key string, _default uint16) uint16

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

func (m SMap) GetUint32(key string, _default uint32) uint32

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

func (m SMap) GetUint64(key string, _default uint64) uint64

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

func (m SMap) GetUint8(key string, _default uint8) uint8

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

func (m SMap) GetUintSlice(key string, _default []uint) []uint

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) Must added in v6.1.0

func (m SMap) Must(key string) interface{}

Must is the same as Get(key), but panic if no the key.

func (SMap) MustBool added in v6.1.0

func (m SMap) MustBool(key string) bool

MustBool is the same as GetBool, but panic if no the key.

func (SMap) MustDuration added in v6.1.0

func (m SMap) MustDuration(key string) time.Duration

MustDuration is the same as GetDuration, but panic if no the key.

func (SMap) MustFloat32 added in v6.1.0

func (m SMap) MustFloat32(key string) float32

MustFloat32 is the same as GetFloat32, but panic if no the key.

func (SMap) MustFloat64 added in v6.1.0

func (m SMap) MustFloat64(key string) float64

MustFloat64 is the same as GetFloat64, but panic if no the key.

func (SMap) MustFloat64Slice added in v6.1.0

func (m SMap) MustFloat64Slice(key string) []float64

MustFloat64Slice is the same as GetFloat64Slice, but panic if no the key.

func (SMap) MustInt added in v6.1.0

func (m SMap) MustInt(key string) int

MustInt is the same as GetInt, but panic if no the key.

func (SMap) MustInt16 added in v6.1.0

func (m SMap) MustInt16(key string) int16

MustInt16 is the same as GetInt16, but panic if no the key.

func (SMap) MustInt32 added in v6.1.0

func (m SMap) MustInt32(key string) int32

MustInt32 is the same as GetInt32, but panic if no the key.

func (SMap) MustInt64 added in v6.1.0

func (m SMap) MustInt64(key string) int64

MustInt64 is the same as GetInt64, but panic if no the key.

func (SMap) MustInt8 added in v6.1.0

func (m SMap) MustInt8(key string) int8

MustInt8 is the same as GetInt8, but panic if no the key.

func (SMap) MustIntSlice added in v6.1.0

func (m SMap) MustIntSlice(key string) []int

MustIntSlice is the same as GetIntSlice, but panic if no the key.

func (SMap) MustInterfaceSlice added in v6.1.0

func (m SMap) MustInterfaceSlice(key string) []interface{}

MustInterfaceSlice is the same as GetInterfaceSlice, but panic if no the key.

func (SMap) MustString added in v6.1.0

func (m SMap) MustString(key string) string

MustString is the same as GetString, but panic if no the key.

func (SMap) MustStringMap added in v6.1.0

func (m SMap) MustStringMap(key string) SMap

MustStringMap is the same as GetStringMap, but panic if no the key.

func (SMap) MustStringSlice added in v6.1.0

func (m SMap) MustStringSlice(key string) []string

MustStringSlice is the same as GetStringSlice, but panic if no the key.

func (SMap) MustTime added in v6.1.0

func (m SMap) MustTime(key string) time.Time

MustTime is the same as GetTime, but panic if no the key.

func (SMap) MustUint added in v6.1.0

func (m SMap) MustUint(key string) uint

MustUint is the same as GetUint, but panic if no the key.

func (SMap) MustUint16 added in v6.1.0

func (m SMap) MustUint16(key string) uint16

MustUint16 is the same as GetUint16, but panic if no the key.

func (SMap) MustUint32 added in v6.1.0

func (m SMap) MustUint32(key string) uint32

MustUint32 is the same as GetUint32, but panic if no the key.

func (SMap) MustUint64 added in v6.1.0

func (m SMap) MustUint64(key string) uint64

MustUint64 is the same as GetUint64, but panic if no the key.

func (SMap) MustUint8 added in v6.1.0

func (m SMap) MustUint8(key string) uint8

MustUint8 is the same as GetUint8, but panic if no the key.

func (SMap) MustUintSlice added in v6.1.0

func (m SMap) MustUintSlice(key string) []uint

MustUintSlice is the same as GetUintSlice, but panic if no the key.

func (SMap) ToMap added in v6.1.0

func (m SMap) ToMap() map[string]interface{}

ToMap predicates itself to map[string]interface{}.

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 NewSetFromInts

func NewSetFromInts(elements ...int) Set

NewSetFromInts returns a new Set.

func NewSetFromSet

func NewSetFromSet(sets ...Set) Set

NewSetFromSet returns a new Set.

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 NewSetFromStrings

func NewSetFromStrings(elements ...string) Set

NewSetFromStrings returns a new Set.

It will ignore the empty string.

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) AddInts

func (s Set) AddInts(elements ...int)

AddInts adds some int elements into the set.

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

func (s Set) AddStrings(elements ...string)

AddStrings adds some string elements into the set.

It will ignore the empty string.

func (Set) Clear

func (s Set) Clear()

Clear removes all the elements from the set.

func (Set) Copy

func (s Set) Copy() Set

Copy returns a copy of the current set.

func (Set) Difference

func (s Set) Difference(others ...Set) Set

Difference returns a new set with elements in the set that are not in the others.

func (Set) DifferenceUpdate

func (s Set) DifferenceUpdate(others ...Set)

DifferenceUpdate updates the set, removing the elements found in others.

func (Set) Equal

func (s Set) Equal(other Set) bool

Equal returns true if s == other.

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) Has

func (s Set) Has(element interface{}) bool

Has returns true if the element is in the set. Or return false.

func (Set) Intersection

func (s Set) Intersection(others ...Set) Set

Intersection returns a new set with elements common to the set and all others.

func (Set) IntersectionUpdate

func (s Set) IntersectionUpdate(others ...Set)

IntersectionUpdate updates the set, keeping only elements found in it and all others.

func (Set) List

func (s Set) List() []interface{}

List converts the set to a list type.

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

func (s Set) RemoveInts(elements ...int)

RemoveInts removes the int elements from the set.

func (Set) RemoveStrings

func (s Set) RemoveStrings(elements ...string)

RemoveStrings removes the string elements from the set.

func (Set) Size

func (s Set) Size() int

Size returns the number of the elements in the set.

func (Set) String added in v6.1.0

func (s Set) String() string

func (Set) SymmetricDifference

func (s Set) SymmetricDifference(other Set) Set

SymmetricDifference returns a new set with elements in either the set or other but not both.

func (Set) SymmetricDifferenceUpdate

func (s Set) SymmetricDifferenceUpdate(other Set)

SymmetricDifferenceUpdate updates the set, keeping only elements found in either set, but not in both.

func (Set) Union

func (s Set) Union(others ...Set) Set

Union returns a new set with elements from the set and all others.

func (Set) UnionUpdate

func (s Set) UnionUpdate(others ...Set)

UnionUpdate updates the set, adding the elements from all others.

func (Set) Walk

func (s Set) Walk(f func(interface{}))

Walk travels the elements of the set.

[DEPRECATED] Please use ForEach().

type VerifyFunc

type VerifyFunc func(value interface{}, _type string) bool

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.

Jump to

Keyboard shortcuts

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