英文:
Delete known element from slice in Go
问题
我正在尝试使用slices包从一个chan []byte
的切片中删除元素。但是我想删除一个已知的值,而不是使用位置来删除,就像这里所示:https://stackoverflow.com/questions/37334119/how-to-delete-an-element-from-a-slice-in-golang。
import "golang.org/x/exp/slices"
var receiverChannels []chan []byte
channel := make(chan []byte)
receiverChannels = append(receiverChannels, channel)
receiverChannels = slices.Delete(receiverChannels, channel)
英文:
I am trying to use the slices package to delete a chan []byte
from a slice of them.
But I have a known value that I want to remove instead of using the position like it shows here https://stackoverflow.com/questions/37334119/how-to-delete-an-element-from-a-slice-in-golang.
import "golang.org/x/exp/slices"
var receiverChannels []chan []byte
channel := make(chan []byte)
receiverChannels = append(receiverChannels, channel)
receiverChannels = slices.Delete(receiverChannels, channel)
答案1
得分: 1
Go 1.18 带有类型参数的解决方案:
func Delete[T comparable](collection []T, el T) []T {
idx := Find(collection, el)
if idx > -1 {
return slices.Delete(collection, idx, idx+1)
}
return collection
}
func Find[T comparable](collection []T, el T) int {
for i := range collection {
if collection[i] == el {
return i
}
}
return -1
}
这段代码使用了类型参数(Type Parameters)来实现泛型编程。Delete
函数用于从切片中删除指定元素,Find
函数用于查找元素在切片中的索引位置。这两个函数都可以适用于不同类型的切片,只需在调用时指定相应的类型参数即可。
英文:
Go 1.18 solution with type parameters:
func Delete[T comparable](collection []T, el T) []T {
idx := Find(collection, el)
if idx > -1 {
return slices.Delete(collection, idx, idx+1)
}
return collection
}
func Find[T comparable](collection []T, el T) int {
for i := range collection {
if collection[i] == el {
return i
}
}
return -1
}
答案2
得分: 0
如果切片的顺序不重要,你可以使用映射(map)代替:
var receiverChannels = make(map[chan []byte]struct{})
channel := make(chan []byte)
receiverChannels[channel] = struct{}{}
delete(receiverChannels, channel)
否则,你需要一个find
辅助函数:
func find(channels []chan []byte, ch chan []byte) int {
for i := range channels {
if channels[i] == ch {
return i
}
}
return -1
}
或者一个通用的find
函数:
func find[T comparable](slice []T, item T) int {
for i := range slice {
if slice[i] == item {
return i
}
}
return -1
}
如果你需要保留切片但顺序不重要,你可以简单地将最后一个元素移动到指定位置并截断切片:
i := find(receiverChannels, channel)
if i != -1 {
if i < len(receiverChannels)-1 {
receiverChannels[i] = receiverChannels[len(receiverChannels)-1]
}
receiverChannels = receiverChannels[:len(receiverChannels)-1]
}
最后,如果顺序很重要:
i := find(receiverChannels, channel)
if i != -1 {
receiverChannels = append(receiverChannels[:i], receiverChannels[i+1:]...)
receiverChannels = receiverChannels[:len(receiverChannels)-1]
}
英文:
If the ordering of the slice is not important, you can use a map instead:
var receiverChannels = make(map[chan []byte]struct{})
channel:=make(chan []byte)
receiverChannels[channel]=struct{}{}
delete(receiverChannels,channel)
Otherwise, you need a find
helper function:
func find(channels []chan []byte, ch chan []byte) int {
for i:=range channels {
if channels[i]==ch {
return i
}
}
return -1
}
Or a generic find
:
func find[T comparable](slice []T, item T) int {
for i := range slice {
if slice[i] == item {
return i
}
}
return -1
}
If you need to keep a slice but ordering is not important, you can simply move the last element and truncate the slice:
i:=find(receiverChannels, channel)
if i!=-1 {
if i<len(receiverChannels)-1 {
receiverChannels[i]=receiverChannels[len(receiverChannels)-1]
}
receiverChannels=receiverChannels[:len(receiverChannels)-1]
}
Last, if ordering is important:
i:=find(receiverChannels, channel)
if i!=-1 {
receiverChannels=append(receiverChannels[:i], receiverChannels[i+1:]...)
receiverChannels=receiverChannels[:len(receiverChannels)-1]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论