This is a creation in Article, where the information may have evolved or changed.
import ("FMT" "Io/ioutil" "Net/http" "Net/url" "Strings") Func httpget () {resp, err := http. Get ("http://www.baidu.com") if err != nil {// handle error}defer resp. Body.close () Body, err := ioutil. ReadAll (resp. Body) if err != nil {// handle error}fmt. Println (String (body))}func httppost () {resp, err := http. Post ("http://www.baidu.com", "application/x-www-form-urlencoded", strings. Newreader ("NAME=CJB")) if err != nil {fmt. PRINTLN (Err)}defer resp. Body.close () Body, err := ioutil. ReadAll (resp. Body) if err != nil {// handle error}fmt. Println (String (body))}func httppostform () {resp, err := http. Postform ("http://www.baidu.com", url. values{"key": {"Value"}, "id": {"123"}}) if err != nil {// handle Error}defer resp. Body.close () Body,&nbsP;err := ioutil. ReadAll (resp. Body) if err != nil {// handle error}fmt. Println (String (body))}func httpdo () {client := &http. Client{}req, err := http. Newrequest ("POST", "http://www.baidu.com", strings. Newreader ("NAME=CJB")) If err != nil {// handle error}req. Header.set ("Content-type", "application/x-www-form-urlencoded") req. Header.set ("Cookie", "Name=anny") resp, err := client. Do (req) defer resp. Body.close () Body, err := ioutil. ReadAll (resp. Body) if err != nil {// handle error}fmt. Println (String (body))}func main () {httpget ()//httppost ()//httppostform ()//httpdo ()}
Complex request (to set HTTP header properties and so on) with the Httpdo method, the other occasions with the get,post,postform can be.