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
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
type logWriter struct{}
func main() {
resp, err := http.Get("http://google.com")
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
links := []string{
"http://google.com",
"http://facebook.com",
"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) {
fmt.Println(string(bs))
fmt.Println("Just wrote this many bytes:", len(bs))
return len(bs), nil
func checkLink(link string, c chan string) {
_, err := http.Get(link)
if err != 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