2022-02-10 22:06:57 +00:00
|
|
|
//go:build !solution
|
2020-02-13 16:00:29 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
2024-06-01 23:47:30 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-06-03 17:25:40 +00:00
|
|
|
type HTTPGetResult struct {
|
2024-06-01 23:47:30 +00:00
|
|
|
Success bool
|
|
|
|
Body string
|
|
|
|
Elapsed time.Duration
|
|
|
|
Size int
|
2024-06-03 17:25:40 +00:00
|
|
|
URL string
|
2024-06-01 23:47:30 +00:00
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
2024-06-03 17:25:40 +00:00
|
|
|
func (result HTTPGetResult) String() string {
|
2024-06-01 23:47:30 +00:00
|
|
|
if result.Success {
|
2024-06-03 17:25:40 +00:00
|
|
|
return fmt.Sprintf("%v\t%v\t%v", result.Elapsed, result.Size, result.URL)
|
2024-06-01 23:47:30 +00:00
|
|
|
} else {
|
|
|
|
return result.Error.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-03 17:25:40 +00:00
|
|
|
func GetHTTPBody(url string, ch chan HTTPGetResult) {
|
2024-06-01 23:47:30 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
2024-06-03 17:25:40 +00:00
|
|
|
ch <- HTTPGetResult{Success: false, Error: err}
|
2024-06-01 23:47:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
data, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-06-03 17:25:40 +00:00
|
|
|
ch <- HTTPGetResult{Success: false, Error: err}
|
2024-06-01 23:47:30 +00:00
|
|
|
return
|
|
|
|
}
|
2024-06-03 17:25:40 +00:00
|
|
|
ch <- HTTPGetResult{
|
2024-06-01 23:47:30 +00:00
|
|
|
Success: true,
|
|
|
|
Body: string(data),
|
|
|
|
Elapsed: time.Since(startTime),
|
|
|
|
Size: len(data),
|
2024-06-03 17:25:40 +00:00
|
|
|
URL: url,
|
2024-06-01 23:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 16:00:29 +00:00
|
|
|
|
2024-06-01 23:47:30 +00:00
|
|
|
func main() {
|
|
|
|
startTime := time.Now()
|
2024-06-03 17:25:40 +00:00
|
|
|
urls, ch := os.Args[1:], make(chan HTTPGetResult)
|
2024-06-01 23:47:30 +00:00
|
|
|
for _, url := range urls {
|
2024-06-03 17:25:40 +00:00
|
|
|
go GetHTTPBody(url, ch)
|
2024-06-01 23:47:30 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < len(urls); i++ {
|
|
|
|
fmt.Println(<-ch)
|
|
|
|
}
|
|
|
|
fmt.Println(time.Since(startTime), "elapsed")
|
2020-02-13 16:00:29 +00:00
|
|
|
}
|