Skip to content

Instantly share code, notes, and snippets.

@RohitAwate
Created May 2, 2019 13:36
Show Gist options
  • Save RohitAwate/ebbbccac3063ae14c2e83564e0bceb75 to your computer and use it in GitHub Desktop.
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)
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()
}
@muathendirangu
Copy link

a really concise and good intro into how waitgroups and mutexes work.

Is there a way you can share the slides

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment