mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
Create http_fetch2.go
This commit is contained in:
30
eBook/examples/chapter_15/http_fetch2.go
Normal file
30
eBook/examples/chapter_15/http_fetch2.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user