Incorrect values inside goroutines when looping - go

I have read through CommonMistakes as well as run my code through the -race flag, but I can't seem to pinpoint what is wrong here:
package main
import (
"fmt"
)
func main() {
i := 1
totalHashFields := 6
for i <= totalHashFields {
Combinations(totalHashFields, i, func(c []int) {
fmt.Println("Outside goroutine:", c)
go func(c []int) {
fmt.Println("Inside goroutine:", c)
}(c)
})
i++
}
}
func Combinations(n, m int, emit func([]int)) {
s := make([]int, m)
last := m - 1
var rc func(int, int)
rc = func(i, next int) {
for j := next; j < n; j++ {
s[i] = j
if i == last {
emit(s)
} else {
rc(i+1, j+1)
}
}
return
}
rc(0, 0)
}
(The Combinations function is a combinations algo for those interested)
Here is some of the output from fmt.Println:
Outside goroutine: [0 1 4]
Inside goroutine: [5 5 5]
Outside goroutine: [0 1 2 3 4 5]
Inside goroutine: [5 5 5 5 5 5]
Basically, even though I'm passing c as a parameter to my anonymous go function, the value is consistently different to the value outside of this scope. In the output above, I expected the 2 "Inside" values to also be [0 1 4] and [0 1 2 3 4 5], respectfully.

The problem is that you goroutines all work on distinc int slices but these share a common backing array: After completing Combinations the slice s will be full of 5s. Your c in main shares the underlying backing array with s.
But your goroutines do not start executing until Combinations is done so once they do start, the will see the final value of s which is just 5s.
Here it does not help to pass in the slice like you did as this makes a proper copy of c but not of the backing array.
Try
Combinations(totalHashFields, i, func(c []int) {
fmt.Println("Outside goroutine:", c)
cpy := make([]int, len(c))
copy(cpy, c)
go func(c []int) {
fmt.Println("Inside goroutine:", c)
}(cpy)
})
to make a "deep copy" of c.

Related

Odd behavior of scopes [duplicate]

This question already has answers here:
My object is not updated even if I use the pointer to a type to update it
(3 answers)
Closed 3 years ago.
Consider the following minimal example:
package main
import "fmt"
type runner interface {
s1(int)
s2(int)
}
type test struct {
x1 []int
x2 []int
}
func (t test) s1(v int) {
t.x1 = append(t.x1, v)
t.s2(v)
}
func (t test) s2(v int) {
t.x2[v] = v
}
func main() {
t := test{
x1: make([]int, 0),
x2: make([]int, 10)}
for i := 0; i < 10; i++ {
t.s1(i)
}
fmt.Println(t)
}
Now if you run it, you will get a result like this:
{[] [0 1 2 3 4 5 6 7 8 9]}
meaning that the x1 array is never populated. Or actually, it is, but resets on each time s1 function exits. s2 works just fine placing items in a pre-defined array.
Does anyone know what exactly is going on here? Is it because of the scope of array amends? It seems a little counter intuitive.
P.S. I do understand that x1 is a slice, where x2 is an actual pre-defined array. My own theory goes that if you work with "slices", they can only be changed within a specific scope, not anywhere else.
Value receiver makes a copy of the type and pass it to the function.
Just make it pointer and you are good to go:
func (t *test) s1(v int) {
t.x1 = append(t.x1, v)
t.s2(v)
}
Output:
&{[0 1 2 3 4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8 9]}
Code:
package main
import "fmt"
type runner interface {
s1(int)
s2(int)
}
type test struct {
x1 []int
x2 []int
}
func (t *test) s1(v int) {
t.x1 = append(t.x1, v)
t.s2(v)
}
func (t test) s2(v int) {
t.x2[v] = v
}
func main() {
t := &test{
x1: make([]int, 0),
x2: make([]int, 10)}
for i := 0; i < 10; i++ {
t.s1(i)
}
fmt.Println(t)
}

Golang channels, order of execution

I'm learning Go, and have run across the following code snippet:
package main
import "fmt"
func sum(a []int, c chan int) {
sum := 0
for _, v := range a {
sum += v
}
c <- sum // send sum to c
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int, 2)
go sum(a[0:3], c)
go sum(a[3:6], c)
x := <-c
y := <-c
// x, y := <-c, <-c // receive from c
fmt.Println(x, y)
}
Output:
-5 17
Program exited.
Can someone please tell me why the 2nd calling of the "sum" function is coming through the channel before the 1st one? It seems to me that the output should be:
17 -5
I've also tested this with an un-buffered channel and it also gives the same order of output. What am I missing?
You are calling the go routine in your code and you can't tell when the routine will end and the value will be passed to the buffered channel.
As this code is asynchronous so whenever the routine will finish it will write the data to the channel and will be read on the other side. In the example above you are calling only two go routines so the behavior is certain and same output is generated somehow for most of the cases but when you will increase the go routines the output will not be same and the order will be different unless you make it synchronous.
Example:
package main
import "fmt"
func sum(a []int, c chan int) {
sum := 0
for _, v := range a {
sum += v
}
c <- sum // send sum to c
}
func main() {
a := []int{7, 2, 8, -9, 4, 2, 4, 2, 8, 2, 7, 2, 99, -32, 2, 12, 32, 44, 11, 63}
c := make(chan int)
for i := 0; i < len(a); i = i + 5 {
go sum(a[i:i+5], c)
}
output := make([]int, 5)
for i := 0; i < 4; i++ {
output[i] = <-c
}
close(c)
fmt.Println(output)
}
The output for this code on different sample runs was
[12 18 0 78 162]
[162 78 12 0 18]
[12 18 78 162 0]
This is because the goroutines wrote the output asynchronously to the buffered channel.
Hope this helps.
When running the golang.org sandbox, I got the same result every time. As stated above. But when I ran the same snippet on in my own sandbox (on my computer), it sometimes changed the order of the threads. This is much more satisfactory. It shows I can't expect any particular order to thread execution, which is intuitive. I just couldn't figure out why I was getting the same order of execution, and it was the reverse of the order the threads were started. I think this was just luck of the draw on the golang.org's sandbox.
Goroutines are started asynchronously and they can write to channel in any order. It is easier to see if you modify your example a little bit:
package main
import (
"fmt"
"time"
)
func sum(a []int, c chan int, name string, sleep int) {
fmt.Printf("started goroutine: %s\n", name)
time.Sleep(time.Second * time.Duration(sleep))
sum := 0
for _, v := range a {
sum += v
}
fmt.Printf("about end goroutine: %s\n", name)
c <- sum // send sum to c
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int, 2)
go sum(a[0:3], c, "A", 1)
go sum(a[3:6], c, "B", 1)
x := <-c
y := <-c
// x, y := <-c, <-c // receive from c
fmt.Println(x, y)
}
https://play.golang.org/p/dK4DT0iUfzY
Result:
started goroutine: B
started goroutine: A
about end goroutine: A
about end goroutine: B
17 -5

Add slice as row dynamically to 2d slice

I'm trying to add the slice sofar to a new row in matrix after each iteration.
func combinations(sofar []int, rest []int, n int, matrix [][]int, count int) {
if n == 0 {
//Next two lines problematic
matrix[count] = append(matrix[count], sofar[0], sofar[1], sofar[2])
count++
fmt.Println(sofar)
} else {
for i := range rest[:len(rest)] {
concat := sofar
concat = append(concat, rest[i])
combinations(concat, rest[i+1:], n-1, matrix, count)
}
}
}
func factorial(x uint) uint {
if x == 0 {
return 1
}
return x * factorial(x-1)
}
Driver program
func triangleNumber() int {
sofar := []int{}
rest := []int{1,2,3,4}
matrixSize := factorial(4)/(factorial(1)*factorial(3))
matrix := make([][]int, matrixSize)
count := 0
fmt.Println(matrixSize)
combinations(sofar, rest, 3, matrix, count)
return 0
}
triangleNumber()
I want matrix to be;
[1 2 3]
[1 2 4]
[1 3 4]
[2 3 4]
But instead it's all going in the first row. Also is there a way I can get rid of count, and just add the slice sofar to the next row?
Actually, there are a couple of things I note with your program:
Append adds to the existing slice at its end (after length), so if you are using append for matrix, you need not allocate a slice of that size (see main in the code below)
After you are adding elements to the matrix, it is simply being dumped in your current program. The combinations function needs to return it back so that when future elements (slice of ints) are added, they are actually all there.
I've added some debugs and remodeled your program a bit, see if it makes sense:
package main
import (
"fmt"
)
func main() {
triangleNumber()
}
func combinations(sofar []int, rest []int, n int, matrix [][]int, count int) [][]int {
fmt.Println("Entered with matrix", matrix)
if n == 0 {
fmt.Println("Entered with count", count)
//Next two lines problematic
matrix = append(matrix, sofar)
count++
fmt.Println(sofar)
fmt.Println("Printing matrix\n***", matrix, "\n***")
return matrix
} else {
for i := range rest[:len(rest)] {
concat := sofar
concat = append(concat, rest[i])
matrix = combinations(concat, rest[i+1:], n-1, matrix, count)
fmt.Println("Sending with count", count)
}
}
return matrix
}
func factorial(x uint) uint {
if x == 0 {
return 1
}
return x * factorial(x-1)
}
func triangleNumber() int {
sofar := []int{}
rest := []int{1, 2, 3, 4}
matrixSize := factorial(4) / (factorial(1) * factorial(3))
matrix := make([][]int, 0)
count := 0
fmt.Println(matrixSize)
combinations(sofar, rest, 3, matrix, count)
return 0
}
And as you can see, you can pretty much get rid of count too with this approach (look at the output). There's still some scope for improvement left though, but I guess this addresses what you were asking.
On playground: https://play.golang.org/p/rnCdPcaIG3N
Hope this helps.
You're adding all to the first row, and you need to add to the next row, see:
Try this (with minimum change to your code: made count a pointer):
package main
import (
"fmt"
)
func main() {
triangleNumber()
}
func triangleNumber() int {
sofar := []int{}
rest := []int{1, 2, 3, 4}
matrixSize := factorial(4) / (factorial(1) * factorial(3))
matrix := make([][]int, matrixSize)
count := 0
fmt.Println(matrixSize)
combinations(sofar, rest, 3, matrix, &count)
fmt.Println(matrix)
return 0
}
func combinations(sofar []int, rest []int, n int, matrix [][]int, count *int) {
if n == 0 {
//Next two lines problematic
matrix[*count] = append(matrix[*count], sofar[0], sofar[1], sofar[2])
*count++
// fmt.Println(count, sofar)
} else {
for i := range rest[:len(rest)] {
concat := sofar
concat = append(concat, rest[i])
combinations(concat, rest[i+1:], n-1, matrix, count)
}
}
}
func factorial(x uint) uint {
if x == 0 {
return 1
}
return x * factorial(x-1)
}
output:
4
[[1 2 3] [1 2 4] [1 3 4] [2 3 4]]
Also for your special case, this works too:
matrix[*count] = []int{sofar[0], sofar[1], sofar[2]}
instead of:
matrix[*count] = append(matrix[*count], sofar[0], sofar[1], sofar[2])

golang implementing generator / yield with channels: odd channel behavior

The following code implements the yield pattern in golang. As an experiment I was implementing an all permutations generator. However, when I return the slice A to channel, if I do not create a new copy of the array I get an incorrect result.
Please see the code around "???". Can someone explain what happens under the covers here? I thought that since the channel is not buffered, I was guaranteed that after publishing the array's slice to the channel I was ensured that the result would be consumed before continuing.
package main
import (
"fmt"
)
func swap(A []int, i int, j int) {
t := A[i]
A[i] = A[j]
A[j] = t
}
func recurse(A []int, c chan []int, depth int) {
if depth == len(A) {
// ??? Why do I need to copy the data?
// If I do c <- A I get an incorrect answer.
ra := make([]int, len(A))
copy(ra, A)
c <- ra
return
}
for i := depth; i < len(A); i++ {
swap(A, depth, i)
recurse(A, c, depth+1)
swap(A, depth, i)
}
}
func yieldPermutations(A []int, c chan []int) {
recurse(A, c, 0)
close(c)
}
func main() {
A := []int{1, 2, 3}
c2 := make(chan []int)
go yieldPermutations(A, c2)
for v := range c2 {
fmt.Println(v)
}
}
If I do not copy the data, I get the following result:
[1 3 2]
[1 3 2]
[2 3 1]
[2 3 1]
[3 1 2]
[3 1 2]
Obviously, the correct result (which we get with data copy) is:
[1 2 3]
[1 3 2]
[2 1 3]
[2 3 1]
[3 2 1]
[3 1 2]
It's a mistake to think this code is like generators/yield in Python, and that's what's causing your error.
In Python, when you request the next item from a generator, the generator starts executing and stops when the next yield <value> statement is reached. There is no parallelism in Python's generators: the consumer runs until it wants a value, then the generator runs until it produces a value, then the consumer gets the value and continues execution.
In your go code, the goroutine executes concurrently with the code that's consuming items. As soon as an item is read from the channel from the main code, the goroutine works concurrently to produce the next. The goroutine and the consumer both run until they reach the channel send/receive, then the value is sent from the goroutine to the consumer, then they both continue execution.
That means the backing array of A gets modified concurrently as the goroutine works to generate the next item. And that's a race condition which causes your unexpected output. To demonstrate that this is a race, insert time.Sleep(time.Second) after the channel send. Then the code produces the correct results (albeit slowly): https://play.golang.org/p/uEa_k6Brcc

I need to assign a variable to itself when iterating for the closure context to keep the correct value

Without i := i, I get an incorrect result (3, 3, 5, 9, 7, 15). With it, I get (0, 0, 3, 3, 6, 10), which is correct. Removing the assignment is similar to getting the value of i at the end of the loop. Why?
package main
import "fmt"
type Handler interface {
Handle(v int)
}
type Elem struct {
Handler Handler
}
var elems []*Elem
type handlerFunc func(v int)
func (h handlerFunc) Handle(v int) { h(v) }
func main() {
newElem := func(fn handlerFunc) {
elem := &Elem{Handler: handlerFunc(fn)}
elems = append(elems, elem)
}
for i := 0; i < 3; i++ {
i := i // *** Why? ***
newElem(func(v int) { fmt.Printf("%d, ", i+v) })
newElem(func(v int) { fmt.Printf("%d, ", i*v) })
}
for n, e := range elems {
if e.Handler != nil {
e.Handler.Handle(n)
}
}
fmt.Printf("\n")
}
The easiest way to visualize what is going on is to change the output of your functions to:
newElem(func(v int) { fmt.Printf("plus %d - %d+%d\n", i+v, i, v) })
newElem(func(v int) { fmt.Printf("times %d - %d*%d\n", i*v, i , v) })
With this change the output becomes:
plus 3 - 3+0
times 3 - 3*1
plus 5 - 3+2
times 9 - 3*3
plus 7 - 3+4
times 15 - 3*5
So, as you can see, i is 3 in all cases. This is because you are creating closures around the i variable, so the functions will use the current value of i when they run, which is 3 by the time the functions actually use i.
You can again see this if you change your code like the following:
http://play.golang.org/p/FRhr0n2oi7
The reason the assignment i := i inside your loop fixes the problem is because you are creating a new variable i inside the scope of your loop which is still closed over by the functions, but doesn't ever change. Each new iteration of the loop creates a new i so no previous i changes value.
Although this document describes a common mistake when dealing with goroutines and closures, it should shed a bit more light on the issue and potential solutions.
https://github.com/golang/go/wiki/CommonMistakes

Resources