slice

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 4 Imported by: 5

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sort

func Sort(slice interface{}, less func(i, j int) bool)

Sort 方便对切片进行排序

Example
package main

import (
	"fmt"

	"github.com/recallsong/go-utils/container/slice"
)

type Persion struct {
	Name string
}

func main() {
	ps := []Persion{
		Persion{Name: "333"},
		Persion{Name: "111"},
		Persion{Name: "444"},
		Persion{Name: "222"},
	}
	slice.Sort(ps, func(i, j int) bool { return ps[i].Name < ps[j].Name })
	fmt.Println(ps)

}
Output:
[{111} {222} {333} {444}]

func Sort2

func Sort2(slice interface{}, less func(i, j int) bool, swap func(i, j int))

Sort2 方便对切片进行排序

func SortInterface

func SortInterface(slice interface{}, less func(i, j int) bool) sort.Interface

SortInterface 返回可以排序的sort.Interface接口

func SortInterface2

func SortInterface2(slice interface{}, less func(i, j int) bool, swap func(i, j int)) sort.Interface

SortInterface2 返回可以排序的sort.Interface接口

Types

type Interfaces

type Interfaces []interface{}

Interfaces 定义方便操作[]interface{}的类型

Example
package main

import (
	"fmt"

	"github.com/recallsong/go-utils/container/slice"
)

func main() {
	s := slice.Interfaces{}
	s.Append(1, 2)
	s.Prepend(-4, -3, -2, -1, 0)
	s.Insert(1, 3, 4, 5, 6)
	fmt.Println(s, s.Len())

	s1 := s.Copy()
	removed := s1.Remove(3, 6)
	fmt.Println(s1, s1.Len(), removed)
}
Output:
[-4 3 4 5 6 -3 -2 -1 0 1 2] 11
[-4 3 4 1 2] 5 6

func (*Interfaces) Append

func (s *Interfaces) Append(data ...interface{})

Append 追加数据

func (Interfaces) Cap

func (s Interfaces) Cap() int

Cap 返回slice容量

func (Interfaces) Copy

func (s Interfaces) Copy() Interfaces

Copy 拷贝一份数据

func (*Interfaces) Insert

func (s *Interfaces) Insert(index int, data ...interface{})

Insert 插入数据

func (Interfaces) Len

func (s Interfaces) Len() int

Len 返回slice长度

func (*Interfaces) Prepend

func (s *Interfaces) Prepend(data ...interface{})

Prepend 向前添加数据

func (*Interfaces) Remove

func (s *Interfaces) Remove(index, num int) int

Remove 从指定位置删除指定数量的数据,返回被删除的数据数量

func (Interfaces) Shuffle

func (s Interfaces) Shuffle() Interfaces

Shuffle 随机打乱slice数据

func (Interfaces) Swap

func (s Interfaces) Swap(i, j int)

Swap 交换两个位置上的数据

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL