From 750eaed481007ac2114289c1c649abf9aba926e5 Mon Sep 17 00:00:00 2001 From: erius Date: Sun, 2 Jun 2024 02:47:30 +0300 Subject: [PATCH] Implemented sum, urlfetch and fetchall tasks --- fetchall/main.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++- sum/sum.go | 2 +- urlfetch/main.go | 22 +++++++++++++++++- wordcount/main.go | 29 +++++++++++++++++++++++- 4 files changed, 106 insertions(+), 4 deletions(-) diff --git a/fetchall/main.go b/fetchall/main.go index 2a10cda..d8603d1 100644 --- a/fetchall/main.go +++ b/fetchall/main.go @@ -2,6 +2,61 @@ package main -func main() { +import ( + "fmt" + "io" + "net/http" + "os" + "time" +) +type HttpGetResult struct { + Success bool + Body string + Elapsed time.Duration + Size int + Url string + Error error +} + +func (result HttpGetResult) String() string { + if result.Success { + return fmt.Sprintf("%v\t%v\t%v", result.Elapsed, result.Size, result.Url) + } else { + return result.Error.Error() + } +} + +func GetHttpBody(url string, ch chan HttpGetResult) { + startTime := time.Now() + resp, err := http.Get(url) + if err != nil { + ch <- HttpGetResult{Success: false, Error: err} + return + } + defer resp.Body.Close() + data, err := io.ReadAll(resp.Body) + if err != nil { + ch <- HttpGetResult{Success: false, Error: err} + return + } + ch <- HttpGetResult{ + Success: true, + Body: string(data), + Elapsed: time.Since(startTime), + Size: len(data), + Url: url, + } +} + +func main() { + startTime := time.Now() + urls, ch := os.Args[1:], make(chan HttpGetResult) + for _, url := range urls { + go GetHttpBody(url, ch) + } + for i := 0; i < len(urls); i++ { + fmt.Println(<-ch) + } + fmt.Println(time.Since(startTime), "elapsed") } diff --git a/sum/sum.go b/sum/sum.go index 1da0e52..b2b97a2 100644 --- a/sum/sum.go +++ b/sum/sum.go @@ -3,5 +3,5 @@ package sum func Sum(a, b int64) int64 { - return 0 + return a + b } diff --git a/urlfetch/main.go b/urlfetch/main.go index 2a10cda..f3377ef 100644 --- a/urlfetch/main.go +++ b/urlfetch/main.go @@ -2,6 +2,26 @@ package main -func main() { +import ( + "fmt" + "io" + "log" + "net/http" + "os" +) +func main() { + urls := os.Args[1:] + for _, url := range urls { + resp, err := http.Get(url) + if err != nil { + log.Fatal("fetch:", err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatal("fetch:", err) + } + fmt.Println(string(body)) + } } diff --git a/wordcount/main.go b/wordcount/main.go index 2a10cda..cc80fb5 100644 --- a/wordcount/main.go +++ b/wordcount/main.go @@ -2,6 +2,33 @@ package main -func main() { +import ( + "bufio" + "fmt" + "os" +) +func LinesCountInFile(lines map[string]int, filename string) { + file, err := os.Open(filename) + if err != nil { + return + } + defer file.Close() + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + lines[line]++ + } +} + +func main() { + filenames, lines := os.Args[1:], make(map[string]int) + for _, filename := range filenames { + LinesCountInFile(lines, filename) + } + for line, count := range lines { + if count != 1 { + fmt.Printf("%v\t%v\n", count, line) + } + } }