GET请求
不带参数直接请求
import (
"fmt"
"net/http"
)
func main(){
resp,err := http.Get("https://www.baidu.com/")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body,err := IoUtil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}
带参数的GET请求
import (原文链接:https://www.f2er.com/go/187863.html
"net/url"
"log"
"net/http"
)
func main(){
u,err := url.Parse("https://www.baidu.com/s")
if err != nil {
log.Fatal(err)
}
q := u.Query()
q.Add("ie","utf-8")
q.Add("wd","golang")
u.RawQuery = q.Encode()
_,err = http.Get(u.String())
if err != nil{
log.Fatal(err)
}
}