From cc6c71ce1517f2018f2f88b87895058149adb438 Mon Sep 17 00:00:00 2001 From: Alexander Pivkin Date: Wed, 8 Oct 2025 11:53:15 +0300 Subject: [PATCH] refactored label generator script and README --- headFirst/routines/go.mod | 3 +++ headFirst/routines/main.go | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 headFirst/routines/go.mod create mode 100644 headFirst/routines/main.go 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()") +}