curl是什么命令Golang实现curl   Linux很常⽤的curl命令,在golang中可以使⽤net/http来实现
模拟get请求
  func Get(url string) (resp *Response, err error)
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
response, err := http.Get("www.baidu")
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
fmt.Println(string(body))
}
模拟POST请求
  func Post(url string, bodyType string, body io.Reader) (resp *Response, err error) package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func main() {
response, err := http.Post(
"localhost/index.php",
"application/x-www-form-urlencoded",
strings.NewReader("name=abc&age=99"),
)
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
fmt.Println(string(body))
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。