Get the content of a web page (golang):
https://www.daniweb.com/programming/computer-science/code/495192/get-the-content-of-a-web-page-golang
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
url := "http://tour.golang.org/welcome/1"
fmt.Printf("HTML code of %s ...\n", url)
resp, err := http.Get(url)
// handle the error if there is one
if err != nil {
panic(err)
}
// do this now so it won't be forgotten
defer resp.Body.Close()
// reads html as a slice of bytes
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// show the HTML code as a string %s
fmt.Printf("%s\n", html)
}
為什麼 Response.Body 需要被關閉?
https://www.jianshu.com/p/407fada3cc9d
簡言之,與其額外處理各種錯誤導致的資源無法回收,使用後就 close Response.Body 是最好的做法。
容易踩到的 50 個坑
35. 關閉 HTTP 的響應體 (譯本這段的程式碼有誤,應參見原文)
https://segmentfault.com/a/1190000013739000#articleHeader38
原文:
http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/
Closing HTTP Response Body
https://stackoverflow.com/questions/32818472/do-we-need-to-close-the-response-object-if-an-error-occurs-while-calling-http-ge
正則處理:
https://www.kancloud.cn/kancloud/web-application-with-golang/44184