tgpl

The Go Programming Language - Solutions
Log | Files | Refs

exercise-1-8.go (472B)


      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 		if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
     23 			resp.Body.Close()
     24 			fmt.Fprintf(os.Stderr, "io.Copy\n")
     25 			os.Exit(1)
     26 		}
     27 	}
     28 }