英文:
Golang edit array of struts from main() to function
问题
希望你能帮忙,以下是我代码的简化版本。
基本上,我将一个结构体数组传递给floatInSlice()
函数,其中要么向数组中添加一个新的结构体,要么将现有结构体的AudienceCategory.Sum
增加1。
总体上都工作得很好,除了b.Sum = b.Sum+1
这一行。
现在我知道,如果我想将一个对象作为指针而不是值传递,我需要使用*
和&
,但是我似乎无法让它工作,非常感谢任何帮助!
type AudienceCategory struct {
Cat int
Sum int
}
var counter = []AudienceCategory{}
func main() {
for i := 1; i < len(words); i++ {
counter = floatInSlice(category, counter)
if counter != nil {}
}
fmt.Printf("%v ", counter)
}
func floatInSlice(category int, counter *[]AudienceCategory) []AudienceCategory {
for _, b := range counter {
if b.Cat == category {
b.Sum = b.Sum+1
return counter
}
}
x := AudienceCategory{Cat: category, Sum: 1}
counter = append(counter, x)
return counter
}
编辑***********
最后终于搞定了,感谢大家的帮助!
func floatInSlice(category int, counter []AudienceCategory) []AudienceCategory {
for i := 0; i < len(counter); i++ {
if counter[i].Cat == category {
counter[i].Sum = counter[i].Sum+1
return counter
}
}
x := AudienceCategory{Cat: category, Sum: 1}
counter = append(counter, x)
return counter
}
英文:
hoping you can help below is a concise version of my code.
basically im passing an array of structs to floatInSlice() in which either a new struct gets added to the array or an existing struct AudienceCategory.Sum gets ++
all is generally working fine except for the b.Sum = b.Sum+1
Now I know that if I want to pass an obect as a pointer rather than value I need to use */& but I just cant seem to get it working, any help much appriciated!
type AudienceCategory struct {
Cat int
Sum int
}
var counter = []AudienceCategory{}
func main() {
for i := 1; i < len(words); i++ {
counter = floatInSlice(category,counter)
if counter != nil{}
}
fmt.Printf("%v ", counter)
}
func floatInSlice(category int, counter *[]AudienceCategory) []AudienceCategory {
for _, b := range counter {
if b.Cat == category {
b.Sum = b.Sum+1
return counter
}
}
x := AudienceCategory{Cat:category,Sum:1}
counter = append( counter, x)
return counter
}
EDIT ***********
got it working in the end thanks for the help guys
func floatInSlice(category int, counter []AudienceCategory) []AudienceCategory {
for i := 0; i < len(counter); i++ {
if counter[i].Cat == category {
counter[i].Sum = counter[i].Sum+1
return counter
}
}
x := AudienceCategory{Cat:category,Sum:1}
counter = append( counter, x)
return counter
}
答案1
得分: 1
当你在_, b := range counter
中使用值范围时,b
包含counter
的每个元素的副本。所以你正在对一个丢失的副本求和,而不是对切片中的元素求和。
你应该使用以下代码:
func floatInSlice(category int, counter *[]AudienceCategory) []AudienceCategory {
for i, b := range *counter {
if b.Cat == category {
(*counter)[i].Sum = (*counter)[i].Sum+1
return counter
}
}
x := AudienceCategory{Cat:category,Sum:1}
counter = append( counter, x)
return counter
}
这样你就可以正确地对切片中的元素进行求和了。
英文:
When you use value range as in _, b := range counter
, b
contains a copy of each element of counter
. So you are summing a copy that is lost, not the element in the slice.
You should use
func floatInSlice(category int, counter *[]AudienceCategory) []AudienceCategory {
for i, b := range *counter {
if b.Cat == category {
(*counter)[i].Sum = (*counter)[i].Sum+1
return counter
}
}
x := AudienceCategory{Cat:category,Sum:1}
counter = append( counter, x)
return counter
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论