This one has come up a couple times for Go
here, but I think my experience is unique. Here are my codes.
type Stack []Weight
func newStack( size int, startSpread Spread ) Stack {
stack := make(Stack, size)
for _, curWeight := range stack {
curWeight = Weight{ startSpread, rand.Float64( ), rand.Float64( ) }
}
return stack
}
Why is gc
telling me I'm not using curWeight
?
Please note that the range construct (for _, curWeight := range stack
) does copy the elements, one after another. So, you are just copying a value, and then you do not use the copy for any further computations, printing or returning. You just drop the copy again.
So I guess your initial idea was to add the weight to the stack and return it. Let`s do that:
func newStack(size int, startSpread Spread) Stack {
stack := make(Stack, size)
for i := 0; i < size; i++ {
stack[i] = Weight{startSpread, rand.Float64(), rand.Float64()}
}
return stack
}