Created
December 14, 2014 15:15
-
-
Save meson10/8f952849ad53e5bee32e to your computer and use it in GitHub Desktop.
Concurrency in golang
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 ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"sync" | |
) | |
const TMP_FILE = "/tmp/urlSink.txt" | |
var URLS = []string{ | |
"http://google.com", | |
"http://duckduckgo.com", | |
"http://search.yahoo.com", | |
} | |
func check(e error) { | |
if e != nil { | |
panic(e) | |
} | |
} | |
func fetch(url string, wait *sync.WaitGroup, sink chan<- []byte) { | |
defer wait.Done() | |
log.Println("Fetching URL", url) | |
resp, err := http.Get(url) | |
if err != nil { | |
check(err) | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
check(err) | |
} | |
sink <- body | |
} | |
func urlSink(sink <-chan []byte, destFile string) { | |
var wg sync.WaitGroup | |
_ = os.Remove(destFile) | |
f, err := os.Create(destFile) | |
check(err) | |
defer f.Close() | |
for v := range sink { | |
wg.Add(1) | |
func(arr []byte) { | |
log.Println("received value in SInk") | |
defer wg.Done() | |
bytes, _ := f.Write(arr) | |
fmt.Printf("wrote %d bytes\n", bytes) | |
check(err) | |
}(v) | |
} | |
f.Sync() | |
wg.Wait() | |
} | |
func main() { | |
destFile := TMP_FILE | |
//Lock group. | |
var results sync.WaitGroup | |
//Prepare a channel where values would be received | |
dataSink := make(chan []byte) | |
go func() { | |
log.Println("Waiting for Values") | |
urlSink(dataSink, destFile) | |
}() | |
for _, url := range URLS { | |
results.Add(1) | |
go fetch(url, &results, dataSink) | |
} | |
results.Wait() | |
close(dataSink) | |
log.Println("All Done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment