Golang edit array of struts from main() to function

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

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 &lt; len(words); i++ {
         counter = floatInSlice(category,counter)
		if counter != nil{}
     }
     fmt.Printf(&quot;%v &quot;, 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 &lt; 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
}

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:

确定