Your code won't work for two (in fact three) reasons:
append
returns a new slice as soon as the capacity is reached.
Thus, the assignment in appendInt
will do nothing.
-
appendInt
runs concurrently, therefore:
- As long as
appendInt
does not message main
that it is finished,
main
does not know when the intSlice
has all the values you want.
- You have to wait for all goroutines to return at the end of
main
Problem 1: Modifying slices in functions
You may know that in Go every value you pass to a function is copied. Reference values,
such as slices, are copied too, but have pointers internally which then point to the original memory location. That means you can modify the elements of a slice in a function. What you
can't do is reassigning this value with a new slice as the internal pointer would point to somewhere different. You need pointers for that. Example (Play):
func modify(s *[]int) {
for i:=0; i < 10; i++ {
*s = append(*s, i)
}
}
func main() {
s := []int{1,2,3}
modify(&s)
fmt.Println(s)
}
Problem 2: Synchronizing goroutines
To wait for started goroutines, you can use a sync.WaitGroup
. Example (Play):
func modify(wg *sync.WaitGroup, s *[]int) {
defer wg.Done()
for i:=0; i < 10; i++ {
*s = append(*s, i)
}
}
func main() {
wg := &sync.WaitGroup{}
s := []int{1,2,3}
wg.Add(1)
go modify(wg, &s)
wg.Wait()
fmt.Println(s)
}
The example above waits (using wg.Wait()
) for modify
to finish
(modify
calls wg.Done()
when finished). If you remove the wg.Wait()
call, you will
see why not synchronizing is a problem. Comparison of outputs:
- With
wg.Wait()
: [1 2 3 0 1 2 3 4 5 6 7 8 9]
- Without
wg.Wait()
: [1 2 3]
The main goroutine returns earlier than the modify
goroutine which is why you will never
see the modified results. Therefore synchronizing is absolutely necessary.
A good way to communicate the new slice would be to use a channel. You would not need to
use pointers and you would have synchronization. Example (Play):
func modify(res chan []int) {
s := []int{}
for i:=0; i < 10; i++ {
s = append(s, i)
}
res <- s
}
func main() {
c := make(chan []int)
go modify(c)
s := <-c
fmt.Println(s)
}