title: "Golang Array Slice操作示例 去重 插入 删除 清空"
date: 2021-02-16T21:21:32+08:00
draft: true
tags: ['go']
author: "dadigang"
author_cn: "大地缸"
personal: "http://www.real007.cn"
关于作者
[Golang]Slice操作示例(去重、插入、删除、清空)
1. Slice去重操作:
/* 在slice中去除重复的元素,其中a必须是已经排序的序列。
* params:
* a: slice对象,如[]string, []int, []float64, ...
* return:
* []interface{}: 已经去除重复元素的新的slice对象
*/
func SliceRemoveDuplicate(a interface{}) (ret []interface{}) {
if reflect.TypeOf(a).Kind() != reflect.Slice {
fmt.Printf("<SliceRemoveDuplicate> <a> is not slice but %T\n", a)
return ret
}
va := reflect.ValueOf(a)
for i := 0; i < va.Len(); i++ {
if i > 0 && reflect.DeepEqual(va.Index(i-1).Interface(), va.Index(i).Interface()) {
continue
}
ret = append(ret, va.Index(i).Interface())
}
return ret
}
运行测试代码:
func test_SliceRemoveDuplicate() {
slice_string := []string{"a", "b", "c", "d", "a", "b", "c", "d"}
slice_int := []int{1, 2, 3, 4, 5, 1, 2, 3, 4, 5}
slice_float := []float64{1.11, 2.22, 3.33, 4.44, 1.11, 2.22, 3.33, 4.44}
sort.Strings(slice_string)
sort.Ints(slice_int)
sort.Float64s(slice_float)
fmt.Printf("slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("slice_float = %v, %p\n", slice_float, slice_float)
ret_slice_string := SliceRemoveDuplicate(slice_string)
ret_slice_int := SliceRemoveDuplicate(slice_int)
ret_slice_float := SliceRemoveDuplicate(slice_float)
fmt.Printf("ret_slice_string = %v, %p\n", ret_slice_string, ret_slice_string)
fmt.Printf("ret_slice_int = %v, %p\n", ret_slice_int, ret_slice_int)
fmt.Printf("ret_slice_float = %v, %p\n", ret_slice_float, ret_slice_float)
fmt.Printf("<after> slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("<after> slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("<after> slice_float = %v, %p\n", slice_float, slice_float)
}
输出结果如下:
slice_string = [a a b b c c d d], 0xc042088000
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e1e0
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc042014200
ret_slice_string = [a b c d], 0xc042034100
ret_slice_int = [1 2 3 4 5], 0xc042088080
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc042034180
<after> slice_string = [a a b b c c d d], 0xc042088000
<after> slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e1e0
<after> slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc042014200
2. Slice插入操作:
/*
* 在Slice指定位置插入元素。
* params:
* s: slice对象,类型为[]interface{}
* index: 要插入元素的位置索引
* value: 要插入的元素
* return:
* 已经插入元素的slice,类型为[]interface{}
*/
func SliceInsert(s []interface{}, index int, value interface{}) []interface{} {
rear := append([]interface{}{}, s[index:]...)
return append(append(s[:index], value), rear...)
}
/*
* 在Slice指定位置插入元素。
* params:
* s: slice对象指针,类型为*[]interface{}
* index: 要插入元素的位置索引
* value: 要插入的元素
* return:
* 无
*/
func SliceInsert2(s *[]interface{}, index int, value interface{}) {
rear := append([]interface{}{}, (*s)[index:]...)
*s = append(append((*s)[:index], value), rear...)
}
/*
* 在Slice指定位置插入元素。
* params:
* s: slice对象的指针,如*[]string, *[]int, ...
* index: 要插入元素的位置索引
* value: 要插入的元素
* return:
* true: 插入成功
* false: 插入失败(不支持的数据类型)
*/
func SliceInsert3(s interface{}, index int, value interface{}) bool {
if ps, ok := s.(*[]string); ok {
if val, ok := value.(string); ok {
rear := append([]string{}, (*ps)[index:]...)
*ps = append(append((*ps)[:index], val), rear...)
return true
}
} else if ps, ok := s.(*[]int); ok {
if val, ok := value.(int); ok {
rear := append([]int{}, (*ps)[index:]...)
*ps = append(append((*ps)[:index], val), rear...)
}
} else if ps, ok := s.(*[]float64); ok {
if val, ok := value.(float64); ok {
rear := append([]float64{}, (*ps)[index:]...)
*ps = append(append((*ps)[:index], val), rear...)
}
} else {
fmt.Printf("<SliceInsert3> Unsupported type: %T\n", s)
}
return false
}
说明:
1. SliceInsert()方法是传入一个[]interface{}类型的slice对象,返回的也是一个[]interface{}类型的slice对象。
2. SliceInsert2()方法是传入一个[]interface{}类型的slice对象指针,直接修改这个slice对象。
3. SliceInsert3()方法是传入一个具体类型的slice对象指针(如*[]string, *[]int等),方法中直接修改这个slice对象,返回操作是否成功的状态(bool)。
运行测试代码:
func test_SliceInsert(m int) {
slice_string := []string{"a", "b", "c", "d", "a", "b", "c", "d"}
slice_int := []int{1, 2, 3, 4, 5, 1, 2, 3, 4, 5}
slice_float := []float64{1.11, 2.22, 3.33, 4.44, 1.11, 2.22, 3.33, 4.44}
sort.Strings(slice_string)
sort.Ints(slice_int)
sort.Float64s(slice_float)
fmt.Printf("slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("slice_float = %v, %p\n", slice_float, slice_float)
ret_slice_string := SliceRemoveDuplicate(slice_string)
ret_slice_int := SliceRemoveDuplicate(slice_int)
ret_slice_float := SliceRemoveDuplicate(slice_float)
//去重之后的结果
fmt.Printf("ret_slice_string = %v, %p\n", ret_slice_string, ret_slice_string)
fmt.Printf("ret_slice_int = %v, %p\n", ret_slice_int, ret_slice_int)
fmt.Printf("ret_slice_float = %v, %p\n", ret_slice_float, ret_slice_float)
switch m {
case 1:
ret1 := SliceInsert(ret_slice_string, 2, "bbb")
ret2 := SliceInsert(ret_slice_int, 2, 222)
ret3 := SliceInsert(ret_slice_float, 2, 222.222)
fmt.Printf("<after insert> slice_string = %v, %p\n", ret1, ret1)
fmt.Printf("<after insert> slice_int = %v, %p\n", ret2, ret2)
fmt.Printf("<after insert> slice_float = %v, %p\n", ret3, ret3)
case 2:
SliceInsert2(&ret_slice_string, 2, "bbb")
SliceInsert2(&ret_slice_int, 2, 222)
SliceInsert2(&ret_slice_float, 2, 222.222)
fmt.Printf("<after insert> slice_string = %v, %p\n", ret_slice_string, ret_slice_string)
fmt.Printf("<after insert> slice_int = %v, %p\n", ret_slice_int, ret_slice_int)
fmt.Printf("<after insert> slice_float = %v, %p\n", ret_slice_float, ret_slice_float)
case 3:
SliceInsert3(&slice_string, 2, "bbb")
SliceInsert3(&slice_int, 2, 222)
SliceInsert3(&slice_float, 2, 222.222)
fmt.Printf("<after insert> slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("<after insert> slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("<after insert> slice_float = %v, %p\n", slice_float, slice_float)
}
}
输出结果如下:
运行test_SliceInsert(1),结果如下:
slice_string = [a a b b c c d d], 0xc042088000
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e1e0
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc042014200
ret_slice_string = [a b c d], 0xc042034100
ret_slice_int = [1 2 3 4 5], 0xc042088080
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc042034180
<after insert> slice_string = [a b bbb c d], 0xc042088100
<after insert> slice_int = [1 2 222 3 4 5], 0xc042088080
<after insert> slice_float = [1.11 2.22 222.222 3.33 4.44], 0xc042088180
运行test_SliceInsert(2),结果如下:
slice_string = [a a b b c c d d], 0xc042088200
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e230
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc042014280
ret_slice_string = [a b c d], 0xc0420341c0
ret_slice_int = [1 2 3 4 5], 0xc042088280
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc042034240
<after insert> slice_string = [a b bbb c d], 0xc042088300
<after insert> slice_int = [1 2 222 3 4 5], 0xc042088280
<after insert> slice_float = [1.11 2.22 222.222 3.33 4.44], 0xc042088380
运行test_SliceInsert(3),结果如下:
slice_string = [a a b b c c d d], 0xc042088400
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e280
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc0420142c0
ret_slice_string = [a b c d], 0xc042034280
ret_slice_int = [1 2 3 4 5], 0xc042088480
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc042034300
<after insert> slice_string = [a a bbb b b c c d d], 0xc04208c000
<after insert> slice_int = [1 1 222 2 2 3 3 4 4 5 5], 0xc04206a0a0
<after insert> slice_float = [1.11 1.11 222.222 2.22 2.22 3.33 3.33 4.44 4.44], 0xc04208a080
3. Slice删除操作
/*
* 删除Slice中的元素。
* params:
* s: slice对象,类型为[]interface{}
* index: 要删除元素的索引
* return:
* 已经删除指定元素的slice,类型为[]interface{}
* 说明:返回的序列与传入的序列地址不发生变化(但是传入的序列内容已经被修改,不能再使用)
*/
func SliceRemove(s []interface{}, index int) []interface{} {
return append(s[:index], s[index+1:]...)
}
/*
* 删除Slice中的元素。
* params:
* s: slice对象指针,类型为*[]interface{}
* index: 要删除元素的索引
* return:
* 无
* 说明:直接操作传入的Slice对象,传入的序列地址不变,但内容已经被修改
*/
func SliceRemove2(s *[]interface{}, index int) {
*s = append((*s)[:index], (*s)[index+1:]...)
}
/*
* 删除Slice中的元素。
* params:
* s: slice对象的指针,如*[]string, *[]int, ...
* index: 要删除元素的索引
* return:
* true: 删除成功
* false: 删除失败(不支持的数据类型)
* 说明:直接操作传入的Slice对象,不需要转换为[]interface{}类型。
*/
func SliceRemove3(s interface{}, index int) bool {
if ps, ok := s.(*[]string); ok {
*ps = append((*ps)[:index], (*ps)[index+1:]...)
} else if ps, ok := s.(*[]int); ok {
*ps = append((*ps)[:index], (*ps)[index+1:]...)
} else if ps, ok := s.(*[]float64); ok {
*ps = append((*ps)[:index], (*ps)[index+1:]...)
} else {
fmt.Printf("<SliceRemove3> Unsupported type: %T\n", s)
return false
}
return true
}
运行测试代码:
func test_SliceRemove(m int) {
fmt.Printf("============test_SliceRemove============== m=%v \n", m)
slice_string := []string{"a", "b", "c", "d", "a", "b", "c", "d"}
slice_int := []int{1, 2, 3, 4, 5, 1, 2, 3, 4, 5}
slice_float := []float64{1.11, 2.22, 3.33, 4.44, 1.11, 2.22, 3.33, 4.44}
sort.Strings(slice_string)
sort.Ints(slice_int)
sort.Float64s(slice_float)
fmt.Printf("slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("slice_float = %v, %p\n", slice_float, slice_float)
ret_slice_string := SliceRemoveDuplicate(slice_string)
ret_slice_int := SliceRemoveDuplicate(slice_int)
ret_slice_float := SliceRemoveDuplicate(slice_float)
//去重之后的结果
fmt.Printf("ret_slice_string = %v, %p\n", ret_slice_string, ret_slice_string)
fmt.Printf("ret_slice_int = %v, %p\n", ret_slice_int, ret_slice_int)
fmt.Printf("ret_slice_float = %v, %p\n", ret_slice_float, ret_slice_float)
switch m {
case 1:
ret1 := SliceRemove(ret_slice_string, 2)
ret2 := SliceRemove(ret_slice_int, 2)
ret3 := SliceRemove(ret_slice_float, 2)
fmt.Printf("<after remove> ret1 = %v, %p\n", ret1, ret1)
fmt.Printf("<after remove> ret2 = %v, %p\n", ret2, ret2)
fmt.Printf("<after remove> ret3 = %v, %p\n", ret3, ret3)
fmt.Printf("<after remove> ret_slice_string = %v, %p\n", ret_slice_string, ret_slice_string)
fmt.Printf("<after remove> ret_slice_int = %v, %p\n", ret_slice_int, ret_slice_int)
fmt.Printf("<after remove> ret_slice_float = %v, %p\n", ret_slice_float, ret_slice_float)
case 2:
SliceRemove2(&ret_slice_string, 2)
SliceRemove2(&ret_slice_int, 2)
SliceRemove2(&ret_slice_float, 2)
fmt.Printf("<after remove> slice_string = %v, %p\n", ret_slice_string, ret_slice_string)
fmt.Printf("<after remove> slice_int = %v, %p\n", ret_slice_int, ret_slice_int)
fmt.Printf("<after remove> slice_float = %v, %p\n", ret_slice_float, ret_slice_float)
case 3:
SliceRemove3(&slice_string, 2)
SliceRemove3(&slice_int, 2)
SliceRemove3(&slice_float, 2)
fmt.Printf("<after remove> slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("<after remove> slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("<after remove> slice_float = %v, %p\n", slice_float, slice_float)
}
}
输出结果如下:
运行test_SliceRemove(1),输出结果如下:
slice_string = [a a b b c c d d], 0xc04209c000
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04209e000
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc04207c040
ret_slice_string = [a b c d], 0xc0420520c0
ret_slice_int = [1 2 3 4 5], 0xc04209c080
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc042052140
<after remove> ret1 = [a b d], 0xc0420520c0
<after remove> ret2 = [1 2 4 5], 0xc04209c080
<after remove> ret3 = [1.11 2.22 4.44], 0xc042052140
<after remove> ret_slice_string = [a b d d], 0xc0420520c0
<after remove> ret_slice_int = [1 2 4 5 5], 0xc04209c080
<after remove> ret_slice_float = [1.11 2.22 4.44 4.44], 0xc042052140
运行test_SliceRemove(2),输出结果如下:
slice_string = [a a b b c c d d], 0xc04209c100
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04209e050
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc04207c080
ret_slice_string = [a b c d], 0xc042052180
ret_slice_int = [1 2 3 4 5], 0xc04209c180
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc042052200
<after remove> slice_string = [a b d], 0xc042052180
<after remove> slice_int = [1 2 4 5], 0xc04209c180
<after remove> slice_float = [1.11 2.22 4.44], 0xc042052200
运行test_SliceRemove(3),输出结果如下:
slice_string = [a a b b c c d d], 0xc04209c200
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04209e0a0
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc04207c0c0
ret_slice_string = [a b c d], 0xc042052240
ret_slice_int = [1 2 3 4 5], 0xc04209c280
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc0420522c0
<after remove> slice_string = [a a b c c d d], 0xc04209c200
<after remove> slice_int = [1 1 2 3 3 4 4 5 5], 0xc04209e0a0
<after remove> slice_float = [1.11 1.11 2.22 3.33 3.33 4.44 4.44], 0xc04207c0c0
4. Slice清空操作
/*
* 清空Slice,传入的slice对象地址发生变化。
* params:
* s: slice对象指针,类型为*[]interface{}
* return:
* 无
*/
func SliceClear(s *[]interface{}) {
*s = append([]interface{}{})
}
/*
* 清空Slice,传入的slice对象地址不变。
* params:
* s: slice对象指针,类型为*[]interface{}
* return:
* 无
*/
func SliceClear2(s *[]interface{}) {
*s = (*s)[0:0]
}
/*
* 清空Slice,传入的slice对象地址不变。
* params:
* s: slice对象的指针,如*[]string, *[]int, ...
* return:
* true: 清空成功
* false: 清空失败(不支持的数据类型)
*/
func SliceClear3(s interface{}) bool {
if ps, ok := s.(*[]string); ok {
*ps = (*ps)[0:0]
//*ps = append([]string{})
} else if ps, ok := s.(*[]int); ok {
*ps = (*ps)[0:0]
//*ps = append([]int{})
} else if ps, ok := s.(*[]float64); ok {
*ps = (*ps)[0:0]
//*ps = append([]float64{})
} else {
fmt.Printf("<SliceClear3> Unsupported type: %T\n", s)
return false
}
return true
}
运行测试代码:
func test_SliceClear(m int) {
slice_string := []string{"a", "b", "c", "d", "a", "b", "c", "d"}
slice_int := []int{1, 2, 3, 4, 5, 1, 2, 3, 4, 5}
slice_float := []float64{1.11, 2.22, 3.33, 4.44, 1.11, 2.22, 3.33, 4.44}
sort.Strings(slice_string)
sort.Ints(slice_int)
sort.Float64s(slice_float)
fmt.Printf("slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("slice_float = %v, %p\n", slice_float, slice_float)
ret_slice_string := SliceRemoveDuplicate(slice_string)
ret_slice_int := SliceRemoveDuplicate(slice_int)
ret_slice_float := SliceRemoveDuplicate(slice_float)
//去重之后的结果
fmt.Printf("ret_slice_string = %v, %p\n", ret_slice_string, ret_slice_string)
fmt.Printf("ret_slice_int = %v, %p\n", ret_slice_int, ret_slice_int)
fmt.Printf("ret_slice_float = %v, %p\n", ret_slice_float, ret_slice_float)
switch m {
case 1:
SliceClear(&ret_slice_string)
SliceClear(&ret_slice_int)
SliceClear(&ret_slice_float)
fmt.Printf("<after clear> ret_slice_string = %v, %p\n", ret_slice_string, ret_slice_string)
fmt.Printf("<after clear> ret_slice_int = %v, %p\n", ret_slice_int, ret_slice_int)
fmt.Printf("<after clear> ret_slice_float = %v, %p\n", ret_slice_float, ret_slice_float)
fmt.Printf("<after clear> slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("<after clear> slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("<after clear> slice_float = %v, %p\n", slice_float, slice_float)
case 2:
SliceClear2(&ret_slice_string)
SliceClear2(&ret_slice_int)
SliceClear2(&ret_slice_float)
fmt.Printf("<after clear> ret_slice_string = %v, %p\n", ret_slice_string, ret_slice_string)
fmt.Printf("<after clear> ret_slice_int = %v, %p\n", ret_slice_int, ret_slice_int)
fmt.Printf("<after clear> ret_slice_float = %v, %p\n", ret_slice_float, ret_slice_float)
fmt.Printf("<after clear> slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("<after clear> slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("<after clear> slice_float = %v, %p\n", slice_float, slice_float)
case 3:
SliceClear3(&slice_string)
SliceClear3(&slice_int)
SliceClear3(&slice_float)
fmt.Printf("<after clear> slice_string = %v, %p\n", slice_string, slice_string)
fmt.Printf("<after clear> slice_int = %v, %p\n", slice_int, slice_int)
fmt.Printf("<after clear> slice_float = %v, %p\n", slice_float, slice_float)
}
}
输出结果如下:
运行test_SliceClear(1)结果:
slice_string = [a a b b c c d d], 0xc042088000
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e1e0
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc042014200
ret_slice_string = [a b c d], 0xc042034100
ret_slice_int = [1 2 3 4 5], 0xc042088080
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc042034180
<after clear> ret_slice_string = [], 0x5b5800
<after clear> ret_slice_int = [], 0x5b5800
<after clear> ret_slice_float = [], 0x5b5800
<after clear> slice_string = [a a b b c c d d], 0xc042088000
<after clear> slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e1e0
<after clear> slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc042014200
运行test_SliceClear(2)结果:
slice_string = [a a b b c c d d], 0xc042088100
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e230
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc042014240
ret_slice_string = [a b c d], 0xc0420341c0
ret_slice_int = [1 2 3 4 5], 0xc042088180
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc042034240
<after clear> ret_slice_string = [], 0xc0420341c0
<after clear> ret_slice_int = [], 0xc042088180
<after clear> ret_slice_float = [], 0xc042034240
<after clear> slice_string = [a a b b c c d d], 0xc042088100
<after clear> slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e230
<after clear> slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc042014240
运行test_SliceClear(3)结果:
slice_string = [a a b b c c d d], 0xc042088200
slice_int = [1 1 2 2 3 3 4 4 5 5], 0xc04200e280
slice_float = [1.11 1.11 2.22 2.22 3.33 3.33 4.44 4.44], 0xc042014280
ret_slice_string = [a b c d], 0xc042034280
ret_slice_int = [1 2 3 4 5], 0xc042088280
ret_slice_float = [1.11 2.22 3.33 4.44], 0xc042034300
<after clear> slice_string = [], 0xc042088200
<after clear> slice_int = [], 0xc04200e280
<after clear> slice_float = [], 0xc042014280
有疑问加站长微信联系(非本文作者)