我建议对icza答案进行编辑,以在其底部提供一个最小的工作代码示例,以说明他提供的有用信息。被拒绝,指出它作为编辑是没有意义的,它应该被写为评论或答案,所以就在这里(主要归功于icza):
最低工作代码示例(带注释的内容):
// use a pointer for the input slice so then it is changed in-placefunc FindAndRemoveFromFooSliceInPlace(iFilter int, inSl *[]FooItem) *FooItem { pointedInSl := *inSl // dereference the pointer so then we can use `append` inLen := len(pointedInSl) for idx, elem := range pointedInSl { if elem.Id == iFilter { log.Printf("Loop ID %v", idx) // check these docs: https://github.com/golang/go/wiki/SliceTricks#delete pointedInSl = append(pointedInSl[:idx], pointedInSl[idx+1:inLen]...) pointedInSl = pointedInSl[:inLen-1] *inSl = pointedInSl // assigning the new slice to the pointed value before returning return &elem } } return nil}


