I'm trying to use slices as a queue data structure and I came up with this implementation, that results in an infinite loop. This is because queue
slice doesn't get updated with sub-slice queue[1:]
.
func badQueue() {
queue := []int{0,1,2,3,4,5}
for len(queue) > 0 {
current, queue := queue[0], queue[1:]
fmt.Println(current, queue)
}
}
0 [1 2 3 4 5]
0 [1 2 3 4 5]
0 [1 2 3 4 5]
0 [1 2 3 4 5]
0 [1 2 3 4 5]
...
I have figured out that the issue is related to the fact that I am re-declaring current
and queue
(with :=
) instead of assigning the value, which fixes the issue:
func goodQueue() {
queue := []int{0,1,2,3,4,5}
var current int
for len(queue) > 0 {
current, queue = queue[0], queue[1:]
fmt.Println(current, queue)
}
}
0 [1 2 3 4 5]
1 [2 3 4 5]
2 [3 4 5]
3 [4 5]
4 [5]
5 []
I know what is causing the issue, but I don't fully understand why re-declaration operation in this case is not working in the same way as assigning. Why queue is not re-declared with sub-slice of queue (queue[1:]
)?
Thanks!
Because you can have multiple variables with the same name, as long as they have different scope. The one in the inner scope will shadow the variable in the outer scope.
So if we break down your example
func badQueue() {
// queue from outer scope, lets call it A
queue := []int{0,1,2,3,4,5}
// the only visible queue here is A, so len(queue) will always refer to A
for len(queue) > 0 {
// same thing here, the only visible queue is A, so queue[0] and queue[1:]
// both refer to A
// We are also declaring new variables, queue and current
// This queue is now shadowing the outer queue, let's call this one B
current, queue := queue[0], queue[1:]
// Here queue will refer to B
fmt.Println(current, queue)
// When the current iteration of the loop ends current and queue B is destroyed
// because they go out of scope and the loop start over with A unchanged
}
}