Browse Source

Create http_fetch2.go

glight2000 10 years ago
parent
commit
0f1e597289
1 changed files with 30 additions and 0 deletions
  1. 30 0
      eBook/examples/chapter_15/http_fetch2.go

+ 30 - 0
eBook/examples/chapter_15/http_fetch2.go

@@ -0,0 +1,30 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"net/http"
+	"os"
+	"strings"
+)
+
+func main() {
+	inputReader := bufio.NewReader(os.Stdin)
+	fmt.Println("Please enter the url...")
+	url, err := inputReader.ReadString('\n')
+	url = strings.TrimSuffix(url, "\r\n")
+	checkError(err)
+	res, err := http.Get(url)
+	checkError(err)
+	data, err := ioutil.ReadAll(res.Body)
+	checkError(err)
+	fmt.Printf("Got: %q", string(data))
+}
+
+func checkError(err error) {
+	if err != nil {
+		log.Fatalf("Get : %v", err)
+	}
+}