Glang's IO operations

  Linux is a very common curl command, which can be implemented using net/http in golang

Simulate a get request

  func Get(url string) (resp *Response, err error)

package main

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

func main() {
	response, err := http.Get("http://www.baidu.com")
	if err != nil {
		panic(err)
	}
	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	fmt.Println(string(body))
}

  

Simulate a POST request

  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(
		"http://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))
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324955953&siteId=291194637