tgpl

The Go Programming Language - Solutions
Log | Files | Refs

commit 60fdf02ddada975fc7e1e041f95397624cd27568
parent 69aecfc83c003bb4696deaf42bd06169b34a982b
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date:   Fri,  1 Mar 2019 02:13:50 +0100

Add exercise 1.9 solution

Diffstat:
Aexercise-1-9.go | 29+++++++++++++++++++++++++++++
1 file changed, 29 insertions(+), 0 deletions(-)

diff --git a/exercise-1-9.go b/exercise-1-9.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "io" + "net/http" + "os" + "strings" +) + +func main() { + prefix := "http://" + for _, url := range os.Args[1:] { + if r := strings.HasPrefix(url, prefix); r == false { + url = prefix + url + } + resp, err := http.Get(url) + if err != nil { + fmt.Fprintf(os.Stderr, "fetch: %v\n", err) + os.Exit(1) + } + fmt.Println(resp.Status) + if _, err := io.Copy(os.Stdout, resp.Body); err != nil { + resp.Body.Close() + fmt.Fprintf(os.Stderr, "io.Copy\n") + os.Exit(1) + } + } +}