Created
August 30, 2019 12:18
-
-
Save rafaelfelini/016edfdfaabf94706bae87e49e478082 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"bytes" | |
"fmt" | |
"runtime" | |
"strconv" | |
"time" | |
) | |
func main() { | |
fmt.Println(getGID()) | |
concurrency := 4 | |
sem := make(chan bool, concurrency) | |
urls := []string{"url1", "url2", "url3", "url4", "url5", "url6", "url7", "url8", "url9", "url10"} | |
for _, url := range urls { | |
sem <- true | |
go func(url string) { | |
defer func() { <-sem }() | |
fmt.Println("Hello, playground", url, getGID()) | |
time.Sleep(10 * time.Second) | |
}(url) | |
} | |
//for i := 0; i < cap(sem); i++ { | |
// sem <- true | |
//} | |
} | |
func getGID() uint64 { | |
b := make([]byte, 64) | |
b = b[:runtime.Stack(b, false)] | |
b = bytes.TrimPrefix(b, []byte("goroutine ")) | |
b = b[:bytes.IndexByte(b, ' ')] | |
n, _ := strconv.ParseUint(string(b), 10, 64) | |
return n | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment