tgpl

The Go Programming Language - Solutions
Log | Files | Refs

exercise-1-9.go (499B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"io"
      6 	"net/http"
      7 	"os"
      8 	"strings"
      9 )
     10 
     11 func main() {
     12 	prefix := "http://"
     13 	for _, url := range os.Args[1:] {
     14 		if r := strings.HasPrefix(url, prefix); r == false {
     15 			url = prefix + url
     16 		}
     17 		resp, err := http.Get(url)
     18 		if err != nil {
     19 			fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
     20 			os.Exit(1)
     21 		}
     22 		fmt.Println(resp.Status)
     23 		if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
     24 			resp.Body.Close()
     25 			fmt.Fprintf(os.Stderr, "io.Copy\n")
     26 			os.Exit(1)
     27 		}
     28 	}
     29 }