Skip to content

Instantly share code, notes, and snippets.

@echohes
Created March 3, 2022 07:09
Show Gist options
  • Save echohes/518d3bf89a0da572b24716b32192c854 to your computer and use it in GitHub Desktop.
Save echohes/518d3bf89a0da572b24716b32192c854 to your computer and use it in GitHub Desktop.
Gorutine sync and change slice
package main
import (
"fmt"
"sync"
)
func test(n int, s []int, w *sync.WaitGroup) {
fmt.Println("start ", n)
for k, v := range s {
s[k] = v * 10
fmt.Println("worker ", n, "slice: ", k, v)
}
w.Done()
}
func main() {
var wg sync.WaitGroup
s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
fmt.Printf("Address of array = %v: %p\n", s, &s)
p1 := 0
p2 := p1 + 2
for k, _ := range [8]int{} {
wg.Add(1)
go test(k, s[p1:p2], &wg)
p1 = p2
p2 = p1 + 2
}
wg.Wait()
fmt.Printf("Address of array = %v: %p\n", s, &s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment