英文:
Inserting Missing value NOT working GoLang
问题
我正在尝试将一个整数值插入到切片中,如果切片中不存在该值。
我的代码:
package main
import (
"fmt"
)
func AppendIfMissing(slice []int, i int) []int {
for _, ele := range slice {
if ele == i {
fmt.Println(i)
return slice
}
}
fmt.Println("i的值是", i)
slice = append(slice, i)
return slice
}
func main() {
slice1 := []int{1, 2, 3, 4}
AppendIfMissing(slice1, 60)
fmt.Println("添加后的切片:", slice1)
}
输出:
i的值是 60
添加后的切片: [1 2 3 4]
切片没有发生追加。我的代码有什么问题?
英文:
I'm trying to Insert a Int value to slice if it is missing in that.
My Code :
package main
import (
"fmt"
)
func AppendIfMissing(slice []int, i int) []int {
for _, ele := range slice {
if ele == i {
fmt.Println(i)
return slice
}
}
fmt.Println("i value is ", i)
slice = append(slice, i)
return slice
}
func main() {
slice1 := []int{1, 2, 3, 4}
AppendIfMissing(slice1, 60)
fmt.Println("slice after adding :", slice1)
}
OutPut :
i value is 60
slice after adding : [1 2 3 4]
Appending to slice is not happening.What is wrong with my code ?
答案1
得分: 12
AppendIfMissing
返回一个切片,你需要将其赋值给一个变量。
append(slice, i)
创建一个新的切片,这意味着参数切片没有被修改,它引用了一个全新的切片:
-
在最后返回的切片
-
需要赋值给一个变量
slice1 = AppendIfMissing(slice1, 60)
我同意文章 "[Arrays, slices (and strings): The mechanics of 'append']" 中提到的内容:
> 尽管切片头部是按值传递的,但头部包含一个指向数组元素的指针,因此原始切片头部和传递给函数的头部副本都描述了同一个数组。
因此,当函数返回时,通过原始切片变量可以看到修改后的元素。
但是该文章中的函数并没有使用 append
:
func AddOneToEachElement(slice []byte) {
for i := range slice {
slice[i]++
}
}
> 切片参数的 内容 可以被函数修改,但是它的 头部 不能被修改。
通过执行
slice = append(slice, i)
你修改了头部,重新分配了结果切片到一个完全不同的数组。
这在函数外部是不可见的。
英文:
AppendIfMissing
returns a slice which you need to affect to a variable.
append(slice, i)
creates a new slice, which means the parameter slice isn't modified, it refers to a all new slice:
-
which is returned at the end
-
which needs to be affected to a variable
slice1 = AppendIfMissing(slice1, 60)
I agree that the article "Arrays, slices (and strings): The mechanics of 'append
'" mentions
> Even though the slice header is passed by value, the header includes a pointer to elements of an array, so both the original slice header and the copy of the header passed to the function describe the same array.
Therefore, when the function returns, the modified elements can be seen through the original slice variable.
But the function in that article wasn't using append
:
func AddOneToEachElement(slice []byte) {
for i := range slice {
slice[i]++
}
}
> the contents of a slice argument can be modified by a function, but its header cannot
And by doing
slice = append(slice, i)
you modify the header, you reallocate the resulting slice to a completely different array.
That won't be visible outside of the function.
答案2
得分: 0
更通用的AppendIfMissing版本,它要求go版本 >= 1.18
func AppendIfMissing[T comparable](slice []T, i T) []T {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}
英文:
More generic version of AppendIfMissing, it requires go version >= 1.18
func AppendIfMissing[T comparable](slice []T, i T) []T {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论