seaguest / util

some auxiliary helpers, IMEI generator/validator, slice operation (union, intersect, contains, export field slice), time formatter...

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

util

some auxiliry utils on IMEI generator/validator, time parse/format, collection operation.

IMEI

Generate

func Generate() string

Generate returns random IMEI

Validate

func Validate(imei string) bool

Validate check if imei is valid.

Slice

Contains

func Contains(list interface{}, e interface{}) bool

Contains check if list contains an element.

// EXAMPLE
var list = []int{1, 1, 2, 3}
var e int = 1

ok := Contains(list, e)
fmt.Println(ok) // true

FieldSlice

func FieldSlice(list interface{}, field string) (reflect.Value, bool)

FieldSlice returns a slice extracted by specifed field from slice.

[1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 5, 6]

[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]

// EXAMPLE
type user struct{
    Name string
    Age uint32
}

var users = []*user{&user{Name:"hello", Age:31}}

z, ok := FieldSlice(users, "Name")
if !ok {
    fmt.Println("Cannot find field")
}

slice, ok := z.Interface().([]string)
if !ok {
    fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // ["hello"] []string

Difference

func Difference(arrs ...interface{}) (reflect.Value, bool)

Difference returns a slice of values that are only present in one of the input slices

[1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 5, 6]

[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]

// EXAMPLE
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}

z, ok := Difference(a, b)
if !ok {
    fmt.Println("Cannot find difference")
}

slice, ok := z.Interface().([]int)
if !ok {
    fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 3, 4] []int

Distinct

func Distinct(arr interface{}) (reflect.Value, bool)

Distinct returns the unique vals of a slice

[1, 1, 2, 3] >> [1, 2, 3]

// EXAMPLE
var a = []int{1, 1, 2, 3}

z, ok := Distinct(a)
if !ok {
    fmt.Println("Cannot find distinct")
}

slice, ok := z.Interface().([]int)
if !ok {
    fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 2, 3] []int

Intersect

func Intersect(arrs ...interface{}) (reflect.Value, bool)

Intersect returns a slice of values that are present in all of the input slices

[1, 1, 3, 4, 5, 6] & [2, 3, 6] >> [3, 6]

[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]

// EXAMPLE
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}

z, ok := Intersect(a, b)
if !ok {
    fmt.Println("Cannot find intersect")
}

slice, ok := z.Interface().([]int)
if !ok {
    fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [2] []int

Union

func Union(arrs ...interface{}) (reflect.Value, bool)

Union returns a slice that contains the unique values of all the input slices

[1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 2, 4, 5, 6]

[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]

// EXAMPLE
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}

z, ok := Union(a, b)
if !ok {
    fmt.Println("Cannot find union")
}

slice, ok := z.Interface().([]int)
if !ok {
    fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 2, 3, 4] []int

Time

ParseTime

func ParseTime(ts interface{}) time.Time

ParseTime parse different time type.

FormatTime

func FormatTime(ts interface{}) string

FormatTime format string to 2006-01-02 15:04:05 format

crypto

AESBase64Encrypt

func AESBase64Encrypt(clear, key, iv string) (encryoted string, err error)

AESBase64Encrypt encrypt clear data with key and iv.

AESBase64Decrypt

func AESBase64Decrypt(encrypted, key, iv string) (clear string, err error)

AESBase64Decrypt decrypt encrypted data with key and iv.

License

MIT

About

some auxiliary helpers, IMEI generator/validator, slice operation (union, intersect, contains, export field slice), time formatter...


Languages

Language:Go 100.0%