分享
golang中对slice操作工具类
u012210379 · · 6435 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
转自https://github.com/astaxie/beeku/blob/master/slice.go,是beego框架的作者写的对slice的操作,很棒
package beeku
import (
"math/rand"
"time"
)
type reducetype func(interface{}) interface{}
type filtertype func(interface{}) bool
func Slice_randList(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
}
func Slice_merge(slice1, slice2 []interface{}) (c []interface{}) {
c = append(slice1, slice2...)
return
}
func In_slice(val interface{}, slice []interface{}) bool {
for _, v := range slice {
if v == val {
return true
}
}
return false
}
func Slice_reduce(slice []interface{}, a reducetype) (dslice []interface{}) {
for _, v := range slice {
dslice = append(dslice, a(v))
}
return
}
func Slice_rand(a []interface{}) (b interface{}) {
randnum := rand.Intn(len(a))
b = a[randnum]
return
}
func Slice_sum(intslice []int64) (sum int64) {
for _, v := range intslice {
sum += v
}
return
}
func Slice_filter(slice []interface{}, a filtertype) (ftslice []interface{}) {
for _, v := range slice {
if a(v) {
ftslice = append(ftslice, v)
}
}
return
}
func Slice_diff(slice1, slice2 []interface{}) (diffslice []interface{}) {
for _, v := range slice1 {
if !In_slice(v, slice2) {
diffslice = append(diffslice, v)
}
}
return
}
func Slice_intersect(slice1, slice2 []interface{}) (diffslice []interface{}) {
for _, v := range slice1 {
if !In_slice(v, slice2) {
diffslice = append(diffslice, v)
}
}
return
}
func Slice_chunk(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
}
func Slice_range(start, end, step int64) (intslice []int64) {
for i := start; i <= end; i += step {
intslice = append(intslice, i)
}
return
}
func Slice_pad(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
}
func Slice_unique(slice []interface{}) (uniqueslice []interface{}) {
for _, v := range slice {
if !In_slice(v, uniqueslice) {
uniqueslice = append(uniqueslice, v)
}
}
return
}
func Slice_shuffle(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
}有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信6435 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
转自https://github.com/astaxie/beeku/blob/master/slice.go,是beego框架的作者写的对slice的操作,很棒
package beeku
import (
"math/rand"
"time"
)
type reducetype func(interface{}) interface{}
type filtertype func(interface{}) bool
func Slice_randList(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
}
func Slice_merge(slice1, slice2 []interface{}) (c []interface{}) {
c = append(slice1, slice2...)
return
}
func In_slice(val interface{}, slice []interface{}) bool {
for _, v := range slice {
if v == val {
return true
}
}
return false
}
func Slice_reduce(slice []interface{}, a reducetype) (dslice []interface{}) {
for _, v := range slice {
dslice = append(dslice, a(v))
}
return
}
func Slice_rand(a []interface{}) (b interface{}) {
randnum := rand.Intn(len(a))
b = a[randnum]
return
}
func Slice_sum(intslice []int64) (sum int64) {
for _, v := range intslice {
sum += v
}
return
}
func Slice_filter(slice []interface{}, a filtertype) (ftslice []interface{}) {
for _, v := range slice {
if a(v) {
ftslice = append(ftslice, v)
}
}
return
}
func Slice_diff(slice1, slice2 []interface{}) (diffslice []interface{}) {
for _, v := range slice1 {
if !In_slice(v, slice2) {
diffslice = append(diffslice, v)
}
}
return
}
func Slice_intersect(slice1, slice2 []interface{}) (diffslice []interface{}) {
for _, v := range slice1 {
if !In_slice(v, slice2) {
diffslice = append(diffslice, v)
}
}
return
}
func Slice_chunk(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
}
func Slice_range(start, end, step int64) (intslice []int64) {
for i := start; i <= end; i += step {
intslice = append(intslice, i)
}
return
}
func Slice_pad(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
}
func Slice_unique(slice []interface{}) (uniqueslice []interface{}) {
for _, v := range slice {
if !In_slice(v, uniqueslice) {
uniqueslice = append(uniqueslice, v)
}
}
return
}
func Slice_shuffle(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
}