refactored label generator script and README
This commit is contained in:
parent
bff28673d3
commit
cc6c71ce15
3
headFirst/routines/go.mod
Normal file
3
headFirst/routines/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module main
|
||||
|
||||
go 1.24.6
|
||||
46
headFirst/routines/main.go
Normal file
46
headFirst/routines/main.go
Normal file
|
|
@ -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()")
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user