mirror of
https://github.com/astaxie/beego.git
synced 2024-11-23 03:10:56 +00:00
167 lines
3.8 KiB
Go
167 lines
3.8 KiB
Go
// Beego (http://beego.me/)
|
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
|
|
|
// @authors astaxie
|
|
|
|
package utils
|
|
|
|
import (
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
type reducetype func(interface{}) interface{}
|
|
type filtertype func(interface{}) bool
|
|
|
|
// InSlice checks given string in string slice or not.
|
|
func InSlice(v string, sl []string) bool {
|
|
for _, vv := range sl {
|
|
if vv == v {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// InSliceIface checks given interface in interface slice.
|
|
func InSliceIface(v interface{}, sl []interface{}) bool {
|
|
for _, vv := range sl {
|
|
if vv == v {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// SliceRandList generate an int slice from min to max.
|
|
func SliceRandList(min, max int) []int {
|
|
if max < min {
|
|
min, max = max, min
|
|
}
|
|
length := max - min + 1
|
|
t0 := time.Now()
|
|
rand.Seed(int64(t0.Nanosecond()))
|
|
list := rand.Perm(length)
|
|
for index, _ := range list {
|
|
list[index] += min
|
|
}
|
|
return list
|
|
}
|
|
|
|
// SliceMerge merges interface slices to one slice.
|
|
func SliceMerge(slice1, slice2 []interface{}) (c []interface{}) {
|
|
c = append(slice1, slice2...)
|
|
return
|
|
}
|
|
|
|
// SliceReduce generates a new slice after parsing every value by reduce function
|
|
func SliceReduce(slice []interface{}, a reducetype) (dslice []interface{}) {
|
|
for _, v := range slice {
|
|
dslice = append(dslice, a(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// SliceRand returns random one from slice.
|
|
func SliceRand(a []interface{}) (b interface{}) {
|
|
randnum := rand.Intn(len(a))
|
|
b = a[randnum]
|
|
return
|
|
}
|
|
|
|
// SliceSum sums all values in int64 slice.
|
|
func SliceSum(intslice []int64) (sum int64) {
|
|
for _, v := range intslice {
|
|
sum += v
|
|
}
|
|
return
|
|
}
|
|
|
|
// SliceFilter generates a new slice after filter function.
|
|
func SliceFilter(slice []interface{}, a filtertype) (ftslice []interface{}) {
|
|
for _, v := range slice {
|
|
if a(v) {
|
|
ftslice = append(ftslice, v)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// SliceDiff returns diff slice of slice1 - slice2.
|
|
func SliceDiff(slice1, slice2 []interface{}) (diffslice []interface{}) {
|
|
for _, v := range slice1 {
|
|
if !InSliceIface(v, slice2) {
|
|
diffslice = append(diffslice, v)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// SliceIntersect returns diff slice of slice2 - slice1.
|
|
func SliceIntersect(slice1, slice2 []interface{}) (diffslice []interface{}) {
|
|
for _, v := range slice1 {
|
|
if !InSliceIface(v, slice2) {
|
|
diffslice = append(diffslice, v)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// SliceChuck separates one slice to some sized slice.
|
|
func SliceChunk(slice []interface{}, size int) (chunkslice [][]interface{}) {
|
|
if size >= len(slice) {
|
|
chunkslice = append(chunkslice, slice)
|
|
return
|
|
}
|
|
end := size
|
|
for i := 0; i <= (len(slice) - size); i += size {
|
|
chunkslice = append(chunkslice, slice[i:end])
|
|
end += size
|
|
}
|
|
return
|
|
}
|
|
|
|
// SliceRange generates a new slice from begin to end with step duration of int64 number.
|
|
func SliceRange(start, end, step int64) (intslice []int64) {
|
|
for i := start; i <= end; i += step {
|
|
intslice = append(intslice, i)
|
|
}
|
|
return
|
|
}
|
|
|
|
// SlicePad prepends size number of val into slice.
|
|
func SlicePad(slice []interface{}, size int, val interface{}) []interface{} {
|
|
if size <= len(slice) {
|
|
return slice
|
|
}
|
|
for i := 0; i < (size - len(slice)); i++ {
|
|
slice = append(slice, val)
|
|
}
|
|
return slice
|
|
}
|
|
|
|
// SliceUnique cleans repeated values in slice.
|
|
func SliceUnique(slice []interface{}) (uniqueslice []interface{}) {
|
|
for _, v := range slice {
|
|
if !InSliceIface(v, uniqueslice) {
|
|
uniqueslice = append(uniqueslice, v)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// SliceShuffle shuffles a slice.
|
|
func SliceShuffle(slice []interface{}) []interface{} {
|
|
for i := 0; i < len(slice); i++ {
|
|
a := rand.Intn(len(slice))
|
|
b := rand.Intn(len(slice))
|
|
slice[a], slice[b] = slice[b], slice[a]
|
|
}
|
|
return slice
|
|
}
|