tgpl

The Go Programming Language - Solutions
Log | Files | Refs

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

Add exercise 1.8 solution

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

diff --git a/exercise-1-8.go b/exercise-1-8.go @@ -0,0 +1,28 @@ +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) + } + if _, err := io.Copy(os.Stdout, resp.Body); err != nil { + resp.Body.Close() + fmt.Fprintf(os.Stderr, "io.Copy\n") + os.Exit(1) + } + } +}