Golang edit array of struts from main() to function

huangapple go评论102阅读模式
英文:

Golang edit array of struts from main() to function

问题

希望你能帮忙,以下是我代码的简化版本。

基本上,我将一个结构体数组传递给floatInSlice()函数,其中要么向数组中添加一个新的结构体,要么将现有结构体的AudienceCategory.Sum增加1。

总体上都工作得很好,除了b.Sum = b.Sum+1这一行。

现在我知道,如果我想将一个对象作为指针而不是值传递,我需要使用*&,但是我似乎无法让它工作,非常感谢任何帮助!

  1. type AudienceCategory struct {
  2. Cat int
  3. Sum int
  4. }
  5. var counter = []AudienceCategory{}
  6. func main() {
  7. for i := 1; i < len(words); i++ {
  8. counter = floatInSlice(category, counter)
  9. if counter != nil {}
  10. }
  11. fmt.Printf("%v ", counter)
  12. }
  13. func floatInSlice(category int, counter *[]AudienceCategory) []AudienceCategory {
  14. for _, b := range counter {
  15. if b.Cat == category {
  16. b.Sum = b.Sum+1
  17. return counter
  18. }
  19. }
  20. x := AudienceCategory{Cat: category, Sum: 1}
  21. counter = append(counter, x)
  22. return counter
  23. }

编辑***********

最后终于搞定了,感谢大家的帮助!

  1. func floatInSlice(category int, counter []AudienceCategory) []AudienceCategory {
  2. for i := 0; i < len(counter); i++ {
  3. if counter[i].Cat == category {
  4. counter[i].Sum = counter[i].Sum+1
  5. return counter
  6. }
  7. }
  8. x := AudienceCategory{Cat: category, Sum: 1}
  9. counter = append(counter, x)
  10. return counter
  11. }
英文:

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!

  1. type AudienceCategory struct {
  2. Cat int
  3. Sum int
  4. }
  5. var counter = []AudienceCategory{}
  6. func main() {
  7. for i := 1; i &lt; len(words); i++ {
  8. counter = floatInSlice(category,counter)
  9. if counter != nil{}
  10. }
  11. fmt.Printf(&quot;%v &quot;, counter)
  12. }
  13. func floatInSlice(category int, counter *[]AudienceCategory) []AudienceCategory {
  14. for _, b := range counter {
  15. if b.Cat == category {
  16. b.Sum = b.Sum+1
  17. return counter
  18. }
  19. }
  20. x := AudienceCategory{Cat:category,Sum:1}
  21. counter = append( counter, x)
  22. return counter
  23. }

EDIT ***********

got it working in the end thanks for the help guys

  1. func floatInSlice(category int, counter []AudienceCategory) []AudienceCategory {
  2. for i := 0; i &lt; len(counter); i++ {
  3. if counter[i].Cat == category {
  4. counter[i].Sum = counter[i].Sum+1
  5. return counter
  6. }
  7. }
  8. x := AudienceCategory{Cat:category,Sum:1}
  9. counter = append( counter, x)
  10. return counter
  11. }

答案1

得分: 1

当你在_, b := range counter中使用值范围时,b包含counter的每个元素的副本。所以你正在对一个丢失的副本求和,而不是对切片中的元素求和。

你应该使用以下代码:

  1. func floatInSlice(category int, counter *[]AudienceCategory) []AudienceCategory {
  2. for i, b := range *counter {
  3. if b.Cat == category {
  4. (*counter)[i].Sum = (*counter)[i].Sum+1
  5. return counter
  6. }
  7. }
  8. x := AudienceCategory{Cat:category,Sum:1}
  9. counter = append( counter, x)
  10. return counter
  11. }

这样你就可以正确地对切片中的元素进行求和了。

英文:

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

  1. func floatInSlice(category int, counter *[]AudienceCategory) []AudienceCategory {
  2. for i, b := range *counter {
  3. if b.Cat == category {
  4. (*counter)[i].Sum = (*counter)[i].Sum+1
  5. return counter
  6. }
  7. }
  8. x := AudienceCategory{Cat:category,Sum:1}
  9. counter = append( counter, x)
  10. return counter
  11. }

huangapple
  • 本文由 发表于 2014年9月12日 02:38:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/25794780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定