diff --git a/headFirst/routines/go.mod b/headFirst/routines/go.mod new file mode 100644 index 0000000..2359d15 --- /dev/null +++ b/headFirst/routines/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.24.6 diff --git a/headFirst/routines/main.go b/headFirst/routines/main.go new file mode 100644 index 0000000..e630597 --- /dev/null +++ b/headFirst/routines/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "io" + "log" + "net/http" +) + +func errCheck(err error) { + if err != nil { + log.Fatalln("Error happened %v\n",err) + } +} + +func responseSize(url string, out chan Page) { + fmt.Println("Getting ",url) + response,err := http.Get(url) + errCheck(err) + defer response.Body.Close() + + body,err := io.ReadAll(response.Body) + errCheck(err) + + out <- Page{URL: url, Size: len(body)} + +} + +type Page struct { + URL string + Size int +} + +func main() { + size := make(chan Page) + urls :=[]string{"https://google.com","https://golang.org","https://golang.org/doc"} + for _,url := range urls { + go responseSize(url,size) + } + fmt.Println("--------") + for i:=0; i < len(urls);i++ { + page := <- size + fmt.Printf("%s: %d\n",page.URL,page.Size) + } + fmt.Println("End main()") +}