Created
March 3, 2022 07:09
-
-
Save echohes/518d3bf89a0da572b24716b32192c854 to your computer and use it in GitHub Desktop.
Gorutine sync and change slice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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