my first question in StackOverflow :D
I am running on go 1.16. I created this function:
func (_m *MyPool) InChannel(outs ...chan interface{}) error {
for _, out := range outs {
out = _m.inChan
}
return nil
}
MyPool is a worker pool type, containing, among other members:
type MyPool struct {
inChan chan interface{}
}
My main problem is Go is flagging the out
loop variable when ranging over the Variadic input of InChannel
. Why is that? I am indeed using it...
Sorry, I am a noob at StackOverflow, so I am editing to clarify a bit. I indeed want to assign, not send. This is because the sender will have an outChan chan interface{}
as a member variable, and will send values via:
func (s *Sender) Out(out interface{}) {
select {
case <-s.Ctx.Done():
return
case s.outChan <- out:
return
}
}
EDIT: So I ended up solving it by doing:
func (m *MyPool) InChannel(outs ...*chan interface{}) error {
for _, out := range outs {
*out = m.inChan
}
return nil
}
You're not "really" using it. You assign something to a variable which you don't read, so the assignment has no effect, so basically you're not using the variable.
Note that =
is assignment. If you want to send something on the channel, use the send statement:
out <- _m.inChan
Or maybe you want to change the value out
represents? out
is a loop variable, it's a copy of the slice elements you range over. Assigning a value to out
only assigns a value to the loop variable, but not to the slice element.
In general you may change slice elements by assigning a value using an index expression like this:
s := []int{1, 2, 3}
for i := range s {
s[i] = 10 // Assign a value to the slice elements
}
In your case however this would do no good, since you're ranging over the slice of the variadic parameters.