Last active
May 7, 2020 15:47
-
-
Save PoplarYang/e7f5b9717b6f83a600f833192aaac048 to your computer and use it in GitHub Desktop.
goroutine和channel
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" | |
| "runtime" | |
| "time" | |
| ) | |
| func main() { | |
| var array [10]int | |
| runtime.GOMAXPROCS(2) // 限制使用CPU的数量 | |
| fmt.Println("Running in", runtime.Version()) // version | |
| for i := 0; i < 10; i++ { | |
| go func(i int) { | |
| for { | |
| array[i]++ | |
| } | |
| }(i) | |
| } | |
| time.Sleep(time.Second) | |
| fmt.Println(array) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment