1. 程式人生 > >Golang]Slice操作示例(去重、插入、刪除、清空)

Golang]Slice操作示例(去重、插入、刪除、清空)

Golang]Slice操作示例(去重、插入、刪除、清空)

https://blog.csdn.net/youngwhz1/article/details/83026263

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

}

  1. 執行測試程式碼:

 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)

}
  1.  

  2.  

輸出結果如下:

 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
  1.  

  2.  

 

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.  

  2.  

說明:
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)

}


}
  1.  

  2.  

輸出結果如下: 

執行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
  1.  

  2.  

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

}

  1. 執行測試程式碼:

 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)

}

}
  1.  

  2.  

輸出結果如下:

 執行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
  1.  

  2.  

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

}
  1.  

  2.  

執行測試程式碼:

 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)

}

}
  1.  

  2.  

輸出結果如下:

執行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
  1.  

  2.