mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-11-13 17:36:12 +08:00
add missing code example of chapter 15.3 (#678)
example code from: https://sites.google.com/site/thewaytogo2012/Downhome/Topic3/code_examples.zip
This commit is contained in:
23
eBook/examples/chapter_15/http_fetch.go
Normal file
23
eBook/examples/chapter_15/http_fetch.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// httpfetch.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
res, err := http.Get("http://www.google.com")
|
||||
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