一句话总结:
切片是动态数组。
数组需要明确指定大小,切片不需要。
数组是值传递,切片是地址传递。
区别
初始化
数组需要明确指定大小,切片不需要。
数组:
a := [...]int{1,2,3}
a := [3]int{1,2,3}
切片:
a:= []int{1,2,3}
a := make([]int, 5)
a := make([]int, 5, 10)
函数传递
数组是值传递,切片是地址传递。
package main
import "fmt"
func changeArray(a [3]int) {
a[0] = 100
}
func changeSlice(s []int) {
s[0] = 100
}
func main() {
//var a [...]int = [...]int{1, 2, 3} //error
a := [...]int{1, 2, 3}
changeArray(a)
fmt.Println(a[0]) //值传递
var a2 [3]int = [3]int{4, 5, 6}
a2[2] = 77
var s []int = []int{1, 2, 3, 4}
fmt.Println(len(s), cap((s)))
s = append(s, 6, 7, 8)
fmt.Println(len(s), cap(s))
changeSlice(s)
fmt.Println(s[0]) //地址传递
}
常用操作
追加
追加元素
arrstar=append(arrstar,元素)
追加切片
arrstar=append(arrstar,array2...)
插入
插入元素
arrend:=append([]int{},array[i:]...)
arrstart:=append(array[:i], ele)
array=append(arrstart, arrend...)
插入切片
arrend:=append([]int{},array[i:]...)
arrstart:=append(array[:i], ele...)
array=append(arrstart, arrend...)
删除
删除元素
ss=append(ss[:index],ss[index+1:]...)
自定义数组
这是一个简单的自定义数组操作,将增、删、改、查、插入已经封装好,并且是线程安全的。
package slice
import (
"fmt"
"reflect"
"errors"
"sync"
)
type Slice struct {
slice []interface{}
_type reflect.Type
rwMutex sync.RWMutex
}
func (this *Slice) Slice() []interface{} {
return this.slice
}
func (this *Slice) Type() reflect.Type {
return this._type
}
func NewSlice(_type reflect.Type) *Slice {
slice := new(Slice)
slice.slice = newSlice()
slice._type = _type
return slice
}
func newSlice() []interface{} {
return make([]interface{}, 0)
}
//添加
func (this *Slice) Append(elem ... interface{}) error {
this.rwMutex.Lock()
defer this.rwMutex.Unlock()
array := newSlice()
elemType := reflect.TypeOf(elem)
if reflect.Slice == elemType.Kind() {
for _, v := range elem {
if this._type != reflect.TypeOf(v) {
return errors.New(fmt.Sprintf("the type %s cannot assign value type %s", elemType, this._type))
}
array = append(array, v)
}
}
this.slice = append(this.slice, array)
return nil
}
//删除
func (this *Slice) Remove(sub int) {
this.rwMutex.Lock()
defer this.rwMutex.Unlock()
this.slice = append(this.slice[:sub], this.slice[sub+1:]...)
}
//替换
func (this *Slice) Replace(sub int, elem interface{}) error {
if len(this.slice) < sub || sub < 0 {
return errors.New(fmt.Sprintf("the slice len is %d, but sub is %d", len(this.slice), sub))
}
if this._type != reflect.TypeOf(elem) {
return errors.New(fmt.Sprintf("the type %s cannot assign value type %s", reflect.TypeOf(elem), this._type))
}
this.rwMutex.Lock()
defer this.rwMutex.Unlock()
this.slice[sub] = elem
return nil
}
//查询
func (this *Slice) Get(sub int) (interface{}, error) {
if len(this.slice) < sub || sub < 0 {
return nil, errors.New(fmt.Sprintf("the slice len is %d, but sub is %d", len(this.slice), sub))
}
this.rwMutex.RLock()
defer this.rwMutex.RUnlock()
return this.slice[sub], nil
}
//插入
func (this *Slice) Insert(sub int, elem ... interface{}) error {
if len(this.slice) < sub || sub < 0 {
return errors.New(fmt.Sprintf("the slice len is %d, but sub is %d", len(this.slice), sub))
}
this.rwMutex.Lock()
defer this.rwMutex.Unlock()
elemType := reflect.TypeOf(elem)
array := newSlice()
array = append(array, this.slice[:sub]...)
if reflect.Slice == elemType.Kind() {
for _, v := range elem {
if this._type != reflect.TypeOf(v) {
return errors.New(fmt.Sprintf("the type %s cannot assign value type %s", elemType, this._type))
}
this.slice = append(this.slice, v)
}
}
this.slice = append(array, this.slice[sub:]...)
return nil
}