invalid operation: work[i] (type *[]bool does not support indexing)

810 views
Skip to first unread message

Игорь Мозговой

unread,
Jul 11, 2015, 7:45:08 AM7/11/15
to golan...@googlegroups.com
Hello everybody. Could you help me? It's my code, and i have got error like in title. How I can to correct this code? Thank you!


func KeyManager (keys []string, work *[]bool, mu *sync.Mutex, remaining []int)(string, int, int){
mu.Lock()
for i:=0; i<len(keys);i++{
if work[i] == false {
work[i] = true
mu.Unlock()
return keys[i], remaining[i] , i
}
}
return "false", -1, -1

}

Ian Lance Taylor

unread,
Jul 11, 2015, 7:56:40 AM7/11/15
to Игорь Мозговой, golang-nuts
The parameter "work" has type "*[]bool", meaning that it is a pointer
to a []bool. When you write work[i] you are trying to index into a
pointer. That doesn't work. That is what the error message is
telling you. Either change work to []bool or change your code to use
(*work)[i].

Note that your function has other problems. You should range in the
loop. You should defer mu.Unlock--it doesn't unlock if the loop falls
through.

Ian

Axel Wagner

unread,
Jul 11, 2015, 7:56:43 AM7/11/15
to Игорь Мозговой, golan...@googlegroups.com
Hi,

why is work a *[]bool in the first place? You don't change the slice
(only it's contents). I don't think, it is necessary, to pass it by
pointer :)

You have two ways to fix this:
a) make work a []bool instead
b) change work[i] to (*work)[i]
I'd prefer a)

Best,

Axel
Reply all
Reply to author
Forward
0 new messages