Created
May 2, 2019 13:36
-
-
Save RohitAwate/ebbbccac3063ae14c2e83564e0bceb75 to your computer and use it in GitHub Desktop.
Code from the YouTube video: Concurrency in Golang: A Simple, Practical Example (https://youtu.be/3atNYmqXyV4)
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" | |
"log" | |
"net/http" | |
"os" | |
"sync" | |
) | |
var wg sync.WaitGroup | |
var mut sync.Mutex | |
func sendRequest(url string) { | |
defer wg.Done() | |
res, err := http.Get(url) | |
if err != nil { | |
panic(err) | |
} | |
mut.Lock() | |
defer mut.Unlock() | |
fmt.Printf("[%d] %s\n", res.StatusCode, url) | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
log.Fatalln("Usage: go run main.go <url1> <url2> .. <urln>") | |
} | |
for _, url := range os.Args[1:] { | |
go sendRequest("https://" + url) | |
wg.Add(1) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a really concise and good intro into how waitgroups and mutexes work.
Is there a way you can share the slides