# Slice slice包包含操作切片的方法集合。
## 源码: [https://github.com/duke-git/lancet/blob/v1/slice/slice.go](https://github.com/duke-git/lancet/blob/v1/slice/slice.go)
## 用法: ```go import ( "github.com/duke-git/lancet/slice" ) ```
## 目录 - [Contain](#Contain) - [ContainSubSlice](#ContainSubSlice) - [Chunk](#Chunk) - [Compact](#Compact) - [Concat](#Concat) - [Count](#Count) - [Difference](#Difference) - [DifferenceBy](#DifferenceBy) - [DeleteByIndex](#DeleteByIndex) - [Drop](#Drop) - [Equal](#Equal) - [EqualWith](#EqualWith) - [Every](#Every) - [Filter](#Filter) - [Find](#Find) - [FindLast](#FindLast) - [FlattenDeep](#FlattenDeep) - [ForEach](#ForEach) - [GroupBy](#GroupBy) - [IntSlice](#IntSlice) - [InterfaceSlice](#InterfaceSlice) - [Intersection](#Intersection) - [InsertByIndex](#InsertByIndex) - [Map](#Map) - [ReverseSlice](#ReverseSlice) - [Reduce](#Reduce) - [Shuffle](#Shuffle) - [SortByField](#SortByField) - [Some](#Some) - [StringSlice](#StringSlice) - [Unique](#Unique) - [Union](#Union) - [UpdateByIndex](#UpdateByIndex) - [Without](#Without)
## 文档 ### Contain

判断slice是否包含value

函数签名: ```go func Contain(iterableType interface{}, value interface{}) bool ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { res := slice.Contain([]string{"a", "b", "c"}, "a") fmt.Println(res) //true } ``` ### ContainSubSlice

判断slice是否包含subslice

函数签名: ```go func ContainSubSlice(slice interface{}, subslice interface{}) bool ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { res := slice.ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "b"}) fmt.Println(res) //true } ``` ### Chunk

按照size参数均分slice

函数签名: ```go func Chunk(slice []interface{}, size int) [][]interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { arr := []string{"a", "b", "c", "d", "e"} res := slice.Chunk(InterfaceSlice(arr), 3) fmt.Println(res) //[][]interface{}{{"a", "b", "c"}, {"d", "e"}} } ``` ### Compact

去除slice中的假值(false values are false, nil, 0, "")

函数签名: ```go func Compact(slice interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { res := slice.Compact([]int{0, 1, 2, 3}) fmt.Println(res) //[]int{1, 2, 3} } ``` ### Concat

连接values到slice中,values类型可以是切片或多个值

函数签名: ```go func Concat(slice interface{}, values ...interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { res1 := slice.Concat([]int{1, 2, 3}, 4, 5) fmt.Println(res1) //[]int{1, 2, 3, 4, 5} res2 := slice.Concat([]int{1, 2, 3}, []int{4, 5}) fmt.Println(res2) //[]int{1, 2, 3, 4, 5} } ``` ### Count

遍历切片,对每个元素执行函数function. 返回符合函数返回值为true的元素的个数,函数签名必须是func(index int, value interface{}) bool

函数签名: ```go func Count(slice, function interface{}) int ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 4, 5, 6} evenFunc := func(i, num int) bool { return (num % 2) == 0 } res := slice.Count(nums, evenFunc) fmt.Println(res) //3 } ``` ### Difference

创建一个切片,其元素不包含在另一个给定切片中

函数签名: ```go func Difference(slice1, slice2 interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { s1 := []int{1, 2, 3, 4, 5} s2 := []int{4, 5, 6} res := slice.Difference(s1, s2) fmt.Println(res) //[]int{1, 2, 3} } ``` ### DifferenceBy

在slice和comparedSlice中的每个元素调用iteratee函数,并比较它们的返回值,如果不想等返回在slice中对应的值

函数签名: ```go func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { s1 := []int{1, 2, 3, 4, 5} s2 := []int{4, 5, 6} addOne := func(i int, v int) int { return v + 1 } res := slice.DifferenceBy(s1, s2, addOne) fmt.Println(res) //[]int{1, 2} } ``` ### DeleteByIndex

删除切片中从开始索引到结束索引-1的元素

函数签名: ```go func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { res1 := slice.DeleteByIndex([]string{"a", "b", "c", "d", "e"}, 3) fmt.Println(res1) //[]string{"a", "b", "c", "e"} res2 := slice.DeleteByIndex([]string{"a", "b", "c", "d", "e"}, 0, 2) fmt.Println(res2) //[]string{"c", "d", "e"} } ``` ### Drop

创建一个切片,当 n > 0 时从开头删除 n 个元素,或者当 n < 0 时从结尾删除 n 个元素

函数签名: ```go func Drop(slice interface{}, n int) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { res1 := slice.Drop([]int{}, 0) fmt.Println(res1) //[]int{} res2 := slice.Drop([]int{1, 2, 3, 4, 5}, 1) fmt.Println(res2) //[]int{2, 3, 4, 5} res3 := slice.Drop([]int{1, 2, 3, 4, 5}, -1) fmt.Println(res3) //[]int{1, 2, 3, 4} } ``` ### Equal

检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同

函数签名: ```go func Equal(slice1, slice2 interface{}) bool ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/v2/slice" ) func main() { slice1 := []int{1, 2, 3} slice2 := []int{1, 2, 3} slice3 := []int{3, 2, 1} res1 := slice.Equal(slice1, slice2) res2 := slice.Equal(slice1, slice3) fmt.Println(res1) //true fmt.Println(res2) //false } ``` ### EqualWith

检查两个切片是否相等,相等条件:对两个切片的元素调用比较函数comparator,返回true。 comparator函数签名: func(a interface{}, b interface{}) bool

函数签名: ```go func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/v2/slice" ) func main() { slice1 := []int{1, 2, 3} slice2 := []int{2, 4, 6} isDouble := func(a, b int) bool { return b == a*2 } res := slice.EqualWith(slice1, slice2, isDouble) fmt.Println(res) //true } ``` ### Every

如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value interface{}) bool

函数签名: ```go func Every(slice, function interface{}) bool ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool { return num%2 == 0 } res := slice.Every(nums, isEven) fmt.Println(res) //false } ``` ### Filter

返回与函数匹配的所有元素。 函数签名应该是 func(index int, value interface{}) bool

函数签名: ```go func Filter(slice, function interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool { return num%2 == 0 } res := slice.Filter(nums, isEven) fmt.Println(res) //[]int{2, 4} } ``` ### Find

遍历slice的元素,返回第一个通过function真值测试的元素。函数签名应该是 func(index int, value interface{}) bool

函数签名: ```go func Find(slice, function interface{}) (interface{}, bool) ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool { return num%2 == 0 } res, ok := slice.Find(nums, even) fmt.Println(res) //2 fmt.Println(ok) //true } ``` ### FindLast

从头到尾遍历 slice 的元素,返回最后一个通过函数真值测试的元素。 函数签名应该是 func(index int, value interface{}) bool。

函数签名: ```go func FindLast(slice, function interface{}) (interface{}, bool) ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool { return num%2 == 0 } res, ok := slice.FindLast(nums, even) fmt.Println(res) //4 fmt.Println(ok) //true } ``` ### FlattenDeep

flattens slice recursive.

函数签名: ```go func FlattenDeep(slice interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}} res := slice.FlattenDeep(arr) fmt.Println(res) //[]string{"a", "b", "c", "d"} } ``` ### ForEach

遍历slice的元素并为每个元素调用函数,函数签名应该是func(index int, value interface{})

函数签名: ```go func ForEach(slice, function interface{}) ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { numbers := []int{1, 2, 3, 4, 5} var numbersAddTwo []int slice.ForEach(numbers, func(index int, value int) { numbersAddTwo = append(numbersAddTwo, value+2) }) fmt.Println(numbersAddTwo) //[]int{3, 4, 5, 6, 7} } ``` ### GroupBy

迭代切片的元素,每个元素将按条件分组,返回两个切片。 函数签名应该是func(index int, value interface{}) bool

函数签名: ```go func GroupBy(slice, function interface{}) (interface{}, interface{}) ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 4, 5, 6} evenFunc := func(i, num int) bool { return (num % 2) == 0 } even, odd := slice.GroupBy(nums, evenFunc) fmt.Println(even) //[]int{2, 4, 6} fmt.Println(odd) //]int{1, 3, 5} } ``` ### IntSlice

将接口切片转换为int切片

函数签名: ```go func IntSlice(slice interface{}) []int ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { var nums = []interface{}{1, 2, 3} res := slice.IntSlice(nums) fmt.Println(res) //[]int{1, 2, 3} } ``` ### InterfaceSlice

将值转换为接口切片

函数签名: ```go func InterfaceSlice(slice interface{}) []interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { var nums = []int{}{1, 2, 3} res := slice.InterfaceSlice(nums) fmt.Println(res) //[]interface{}{1, 2, 3} } ``` ### Intersection

多个切片的交集

函数签名: ```go func Intersection(slices ...interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { s1 := []int{1, 2, 2, 3} s2 := []int{1, 2, 3, 4} res := slice.Intersection(s1, s2), fmt.Println(res) //[]int{1, 2, 3} } ``` ### InsertByIndex

将元素插入到索引处的切片中

函数签名: ```go func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { s := []string{"a", "b", "c"} res1, _ := slice.InsertByIndex(s, 0, "1") fmt.Println(res1) //[]string{"1", "a", "b", "c"} res2, _ := slice.InsertByIndex(s, 3, []string{"1", "2", "3"}) fmt.Println(res2) //[]string{"a", "b", "c", "1", "2", "3"} } ``` ### Map

通过运行函数slice中的每个元素来创建一个值切片,函数签名应该是func(index int, value interface{}) interface{}。

函数签名: ```go func Map(slice, function interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 4} multiplyTwo := func(i, num int) int { return num * 2 } res := slice.Map(nums, multiplyTwo) fmt.Println(res) //[]int{2, 4, 6, 8} } ``` ### ReverseSlice

反转切片中的元素顺序

函数签名: ```go func ReverseSlice(slice interface{}) ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 4} slice.ReverseSlice(nums) fmt.Println(res) //[]int{4, 3, 2, 1} } ``` ### Reduce

将slice中的元素运行函数,返回运行结果。函数签名应该是func(index int, value1, value2 interface{}) interface{}。

函数签名: ```go func Reduce(slice, function, zero interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 4} reduceFunc := func(i, v1, v2 int) int { return v1 + v2 } res := slice.Reduce(nums, reduceFunc, 0) fmt.Println(res) //10 } ``` ### Shuffle

随机打乱切片中的元素顺序

函数签名: ```go func Shuffle(slice interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 4, 5} res := slice.Shuffle(nums) fmt.Println(res) //3,1,5,4,2 } ``` ### SortByField

按字段对结构切片进行排序。slice元素应为struct,字段类型应为int、uint、string或bool。 默认排序类型是升序(asc),如果是降序,设置 sortType 为 desc

函数签名: ```go func SortByField(slice interface{}, field string, sortType ...string) error ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { type student struct { name string age int } students := []student{ {"a", 10}, {"b", 15}, {"c", 5}, {"d", 6}, } err := slice.SortByField(students, "age", "desc") if err != nil { fmt.Println(err) } fmt.Println(students) // []students{ // {"b", 15}, // {"a", 10}, // {"d", 6}, // {"c", 5}, // } } ``` ### Some

如果列表中的任何值通过谓词函数,则返回true,函数签名应该是func(index int, value interface{}) bool .

函数签名: ```go func Some(slice, function interface{}) bool ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool { return num%2 == 0 } res := slice.Some(nums, isEven) fmt.Println(res) //true } ``` ### StringSlice

将接口切片转换为字符串切片

函数签名: ```go func StringSlice(slice interface{}) []string ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { var s = []interface{}{"a", "b", "c"} res := slice.StringSlice(s) fmt.Println(res) //[]string{"a", "b", "c"} } ``` ### Unique

删除切片中的重复元素

函数签名: ```go func Unique(slice interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { res := slice.Unique([]int{1, 2, 2, 3}) fmt.Println(res) //[]int{1, 2, 3} } ``` ### Unique

从所有给定的切片按顺序创建一个唯一值切片。 使用 == 进行相等比较。

函数签名: ```go func Union(slices ...interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { s1 := []int{1, 3, 4, 6} s2 := []int{1, 2, 5, 6} res := slice.Union(s1, s2) fmt.Println(res) //[]int{1, 3, 4, 6, 2, 5} } ``` ### UpdateByIndex

更新索引处的切片元素。 如果 param index < 0 或 index >= len(slice),将返回错误

函数签名: ```go func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { s := []string{"a", "b", "c"} res1, _ := slice.UpdateByIndex(s, 0, "1") fmt.Println(res1) //[]string{"1", "b", "c"} } ``` ### Without

创建一个不包括所有给定值的切片

函数签名: ```go func Without(slice interface{}, values ...interface{}) interface{} ``` 例子: ```go import ( "fmt" "github.com/duke-git/lancet/slice" ) func main() { res := slice.Without([]int{1, 2, 3, 4, 5}, 1, 2) fmt.Println(res) //[]int{3, 4, 5} } ```