This is the example you can find in the documentation link:
packagemainimport("sync")typehttpPkgstruct{}func(httpPkg)Get(urlstring){}varhttphttpPkgfuncmain(){varwgsync.WaitGroupvarurls=[]string{"http://www.golang.org/","http://www.google.com/","http://www.somestupidname.com/",}for_,url:=rangeurls{// Increment the WaitGroup counter.wg.Add(1)// Launch a goroutine to fetch the URL.gofunc(urlstring){// Decrement the counter when the goroutine completes.deferwg.Done()// Fetch the URL.http.Get(url)}(url)}// Wait for all HTTP fetches to complete.wg.Wait()}
You probably need to use a WaitGroup.
This is the example you can find in the documentation link:
WaitGroup is awesome 🎉
I was not aware of
WaitGroup
. Thanks!