I was experimenting with goroutines, and it seems I can't modify the value of a struct within a goroutine (example below). Is there any work around for this?
EDIT: It appears that the code runs if I place a sleep statement, indicating that the goroutines would run if given more time, but they finish running after everything in main() has already executed. How do I "wait" for my goroutines to finish before proceeding?
package main
import (
"fmt"
)
type num struct {
val int
}
func (d *num) cube_val() {
fmt.Println("changing value...")
d.val = d.val*d.val*d.val
}
func main() {
a := []num{num{1},num{3},num{2},num{5},num{4}}
for i := range a {
go a[i].cube_val()
}
// code that waits for go routines to finish should get inserted here ...
fmt.Println(a) // change does NOT happen
for i := range a {
a[i].cube_val()
}
fmt.Println(a) // change happens, and fmt.Println statements worked?
}