如何从用户定义结构的用户定义切片中删除用户定义结构?像这样的东西:type someStruct struct { someOtherStruct *typeOfOtherStruct someInt int someString string}var someStructs []someStructfunc someFunc(ss someStruct, ssSlice someStructs) { // .. want to remove ss from ssSlice}我可能应该循环直到找到索引,然后将其删除。但是我如何比较结构?
1 回答
吃鸡游戏
TA贡献1829条经验 获得超7个赞
您找到该元素并制作一个减去该索引的新切片。
package main
import "fmt"
type someStruct struct {
someInt int
someString string
}
func removeIt(ss someStruct, ssSlice []someStruct) []someStruct {
for idx, v := range ssSlice {
if v == ss {
return append(ssSlice[0:idx], ssSlice[idx+1:]...)
}
}
return ssSlice
}
func main() {
someStructs := []someStruct{
{1, "one"},
{2, "two"},
{3, "three"},
}
fmt.Println("Before:", someStructs)
someStructs = removeIt(someStruct{2, "two"}, someStructs)
fmt.Println("After:", someStructs)
}
- 1 回答
- 0 关注
- 252 浏览
添加回答
举报
0/150
提交
取消