My purpose is to remove one element from specific slice, and the code is something like:
func main() {
s := []int{0, 1, 2, 3, 4}
remove(s, 3)
fmt.Println(s, len(s), cap(s))
}
func remove(s []int, idx int) {
if idx < 0 || idx >= len(s) {
return
}
copy(s[idx:], s[idx+1:])
s = s[:len(s)-1]
fmt.Println(s, len(s), cap(s))
}
but the output showed:
[0 1 2 4] 4 5
[0 1 2 4 4] 5 5
As I know, slice will be passed to a function call as reference type, why it is not able to modify it?