The NET/HTTP package in Golang provides the encapsulation of HTTP-related operations. Further encapsulation of THE GET and POST methods is more convenient to use. Other request methods require us to call the underlying implementation

package main

import (
   "fmt"
   "io/ioutil"
   "net/http"
)

func get(a){
   resp, err := http.Get("http://httpbin.org/get")
   iferr ! =nil {
      panic(err)
   }
   defer func(a) {_ = resp.Body.Close()}()

   content, err := ioutil.ReadAll(resp.Body)
   iferr ! =nil {
      panic(err)
   }
   fmt.Printf("%s", content)
   / / {
   // "args": {},
   // "headers": {
   // "Accept-Encoding": "gzip",
   // "Host": "httpbin.org",
   / / "the user-agent" : "Go - HTTP client / 1.1",
   // "X-Amzn-Trace-Id": "Root=1-60e4663e-4a475772249555e35a89632c"
   / /},
   / / "origin", "222.211.214.252",
   // "url": "http://httpbin.org/get"
   / /}
}

func post(a){
   resp, err := http.Post("http://httpbin.org/post"."".nil)
   iferr ! =nil {
      panic(err)
   }
   defer func(a) {_ = resp.Body.Close()}()

   content, err := ioutil.ReadAll(resp.Body)
   iferr ! =nil {
      panic(err)
   }
   fmt.Printf("%s", content)
   / / {
   // "args": {},
   // "data": "",
   // "files": {},
   // "form": {},
   // "headers": {
   // "Accept-Encoding": "gzip",
   // "Content-Length": "0",
   // "Host": "httpbin.org",
   / / "the user-agent" : "Go - HTTP client / 1.1",
   // "X-Amzn-Trace-Id": "Root=1-60e466bc-19f2a05e219847055d72f159"
   / /},
   // "json": null,
   / / "origin", "222.211.214.252",
   // "url": "http://httpbin.org/post"
   / /}
}

func put(a){
   request, err := http.NewRequest(http.MethodPut, "http://httpbin.org/put".nil)
   iferr ! =nil {
      panic(err)
   }
   resp, err := http.DefaultClient.Do(request)
   iferr ! =nil {
      panic(err)
   }
   defer func(a) {_ = resp.Body.Close()}()

   content, err := ioutil.ReadAll(resp.Body)
   iferr ! =nil {
      panic(err)
   }
   fmt.Printf("%s", content)
   / / {
   // "args": {},
   // "data": "",
   // "files": {},
   // "form": {},
   // "headers": {
   // "Accept-Encoding": "gzip",
   // "Content-Length": "0",
   // "Host": "httpbin.org",
   / / "the user-agent" : "Go - HTTP client / 1.1",
   // "X-Amzn-Trace-Id": "Root=1-60e467e5-4db5430f5a0aa8ea75f5f805"
   / /},
   // "json": null,
   / / "origin", "222.211.214.252",
   // "url": "http://httpbin.org/put"
   / /}
}

func delete(a){
   request, err := http.NewRequest(http.MethodDelete, "http://httpbin.org/delete".nil)
   iferr ! =nil {
      panic(err)
   }
   resp, err := http.DefaultClient.Do(request)
   iferr ! =nil {
      panic(err)
   }
   defer func(a) {_ = resp.Body.Close()}()

   content, err := ioutil.ReadAll(resp.Body)
   iferr ! =nil {
      panic(err)
   }
   fmt.Printf("%s", content)
   / / {
   // "args": {},
   // "data": "",
   // "files": {},
   // "form": {},
   // "headers": {
   // "Accept-Encoding": "gzip",
   // "Host": "httpbin.org",
   / / "the user-agent" : "Go - HTTP client / 1.1",
   // "X-Amzn-Trace-Id": "Root=1-60e4683b-6e4e08c400343a9f29a735fe"
   / /},
   // "json": null,
   / / "origin", "222.211.214.252",
   // "url": "http://httpbin.org/delete"
   / /}
}

func main(a){
   get()
   post()
   put()
   delete()}Copy the code