finished 1.7 - 1.9 as well as wrote fetch.go

This commit is contained in:
Derek McQuay 2015-12-04 13:29:31 -08:00
parent 914dca955d
commit bbefd172c1
4 changed files with 119 additions and 0 deletions

26
ch1/dmfetch/1_7.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"io"
"log"
"net/http"
"os"
)
func main() {
if len(os.Args[1:]) < 1 {
log.Fatal("requires args")
}
for _, i := range os.Args[1:] {
resp, err := http.Get(i)
if err != nil {
log.Print(err)
continue
}
defer resp.Body.Close()
_, err = io.Copy(os.Stdout, resp.Body)
if err != nil {
log.Print(err)
}
}
}

32
ch1/dmfetch/1_8.go Normal file
View File

@ -0,0 +1,32 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func main() {
if len(os.Args[1:]) < 1 {
log.Fatal("requires args")
}
for _, i := range os.Args[1:] {
if !strings.HasPrefix(i, "http://") {
i = "http://" + i
}
resp, err := http.Get(i)
if err != nil {
log.Print(err)
continue
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Print(err)
}
fmt.Printf("%s", body)
}
}

33
ch1/dmfetch/1_9.go Normal file
View File

@ -0,0 +1,33 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func main() {
if len(os.Args[1:]) < 1 {
log.Fatal("requires args")
}
for _, i := range os.Args[1:] {
if !strings.HasPrefix(i, "http://") {
i = "http://" + i
}
resp, err := http.Get(i)
if err != nil {
log.Print(err)
continue
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Print(err)
}
fmt.Printf("%s", body)
fmt.Printf("HTTP status code: %s\n", resp.Status)
}
}

28
ch1/dmfetch/main.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
if len(os.Args[1:]) < 1 {
log.Fatal("requires args")
}
for _, i := range os.Args[1:] {
resp, err := http.Get(i)
if err != nil {
log.Print(err)
continue
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Print(err)
}
fmt.Printf("%s", body)
}
}