Commit 921f82b4 authored by Muhammad, Hammad's avatar Muhammad, Hammad

added code related to channels and go routines

parent 47736d3d
...@@ -2,27 +2,41 @@ package main ...@@ -2,27 +2,41 @@ package main
import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
"os" "time"
) )
type logWriter struct{}
func main() { func main() {
resp, err := http.Get("http://google.com") links := []string{
if err != nil { "http://google.com",
fmt.Println("Error:", err) "http://facebook.com",
os.Exit(1) "http://stackoverflow.com",
"http://golang.org",
"http://amazon.com",
} }
lw := logWriter{} c := make(chan string)
for _, link := range links {
go checkLink(link, c)
}
io.Copy(lw, resp.Body) for l := range c {
go func(link string) {
time.Sleep(5 * time.Second)
checkLink(link, c)
}(l)
}
} }
func (logWriter) Write(bs []byte) (int, error) { func checkLink(link string, c chan string) {
fmt.Println(string(bs)) _, err := http.Get(link)
fmt.Println("Just wrote this many bytes:", len(bs)) if err != nil {
return len(bs), nil fmt.Println(link, "might be down!")
c <- link
return
}
fmt.Println(link, "is up!")
c <- link
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment