golang中對slice操作工具類

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

轉自https://github.com/astaxie/beeku/blob/master/slice.go,是beego架構的作者寫的對slice的操作,很棒

package beekuimport ("math/rand""time")type reducetype func(interface{}) interface{}type filtertype func(interface{}) boolfunc Slice_randList(min, max int) []int {if max < min {min, max = max, min}length := max - min + 1t0 := 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 := sizefor 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}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.