![](/img/trans.png)
[英]Golang append slice to a slice of slice, then the slice will always affect the slice of slice?
[英]GOLANG Delete a slice from Slice of Slice
我想創建 function 從切片中刪除切片。 它應該有兩個輸入:1. 切片切片(list var)和 2. 要刪除的切片(eachsvc)作為輸入。 並返回更新的切片切片(列表變量)。
我遇到了此代碼的問題,因為它不適用於切片切片。 任何幫助表示贊賞。 謝謝。
func truncate_slice(list [][]string, eachsvc []string) [][]string {
//find index of eachsvc in list
i := indexOf(eachsvc, list)
copy(list[i:], list[i+1:]) // Shift a[i+1:] left one index.
list[len(list)-1] = "" // Erase last element (write zero value).
list = list[:len(list)-1] // Truncate slice.
return list
}
函數獲取要從切片中刪除的切片索引
func indexOf(element []string, data [][]string) int {
for k, v := range data {
if element == v {
return k
}
}
return -1 //not found.
}
由於您想根據它們的元素比較切片(正如您評論的那樣),您首先需要定義一個 function 來檢查 2 個給定切片的相等性。 像這樣的東西:
func eq(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
s2Map := make(map[string]int)
s1Map := make(map[string]int)
for _, str := range s2 {
s2Map[str] += 1
}
for _, str := range s1 {
s1Map[str] += 1
}
for key, count := range s1Map {
if count != s2Map[key] {
return false
}
}
return true
}
所以在這種情況下, ["John", "Doe"]
等於["Doe", "John"]
。 如果您還想檢查訂單,我建議您使用reflect.DeepEqual(slice1, slice2)
而不是實現一個。 順便說一句,使用==
來比較切片,意味着它們是否具有相同的參考,因為切片基本上是 arrays 的視圖。
正如 AminMal 所說,您可以使用“ reflect.DeepEqual(slice1, slice2) ”來比較切片。
根據文檔:
當滿足以下所有條件時,切片值是完全相等的:它們都是 nil 或都非 nil,它們具有相同的長度,並且它們指向同一底層數組的相同初始條目(即 &x[0 ] == &y[0]) 或其對應的元素(直到長度)非常相等。 請注意,非 nil 空切片和 nil 切片(例如,[]byte{} 和 []byte(nil))並不完全相等。
package main
import (
"errors"
"fmt"
"reflect"
)
func main() {
sl := [][]string{[]string{"test1"}, []string{"test1", "test2"}, []string{"test3"}}
truncSlic, err := truncate_slice(sl, []string{"test3"})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(truncSlic)
}
func indexOf(element []string, data [][]string) int {
for i, v := range data {
if reflect.DeepEqual(element, v) { //compare two silces
return i
}
}
return -1
}
func truncate_slice(list [][]string, eachsvc []string) ([][]string, error) {
//find index of eachsvc in list
i := indexOf(eachsvc, list)
if i == -1 {
return nil, errors.New("Not Found")
}
copy(list[i:], list[i+1:]) // Shift a[i+1:] left one index.
list[len(list)-1] = nil // Erase last element (write zero value).
list = list[:len(list)-1] // Truncate slice.
return list, nil
}
Output: [[test1] [test1 test2]]
element == v 無效,因為運算符 == 沒有在 []string 上定義,所以你應該定義它:
func truncate_slice(list [][]string, eachsvc []string) [][]string {
//find index of eachsvc in list
i := indexOf(eachsvc, list)
copy(list[i:], list[i+1:]) // Shift a[i+1:] left one index.
list[len(list)-1] = nil // Erase last element (write zero value).
list = list[:len(list)-1] // Truncate slice.
return list
}
func indexOf(element []string, data [][]string) int {
for k, v := range data {
if equals(element, v) {
return k
}
}
return -1 //not found.
}
func equals(l1, l2 []string) bool {
if len(l1) != len(l2) {
return false
}
for i := 0; i < len(l1); i++ {
if l1[i] != l2[i] {
return false
}
}
return true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.