This is a creation in Article, where the information may have evolved or changed.
Most of the restful requests can be connected with a short connection, that is, three handshake to establish a link, the exchange of data after the completion of the release of the link, the short link will not take longer to occupy the port number, the actual project will also use another, long links, such as the client sends a restful request, the need to monitor a resource change situation, The server provides a watch mechanism to notify the client when resources change.
So client side, relative to the short link, long links should be how to write?
As with short links, you only need to loop through the response returned by the server side .
package mainimport ( "fmt" "io" "log" "net/http")func main() { request, err := http.NewRequest("GET", "http://www.example.com/", nil) if err != nil { log.Fatal(err) } http_client := &http.Client{} response, err := http_client.Do(request) if err != nil { log.Fatal(err) } buf := make([]byte, 4096) // any non zero value will do, try '1'. for { n, err := response.Body.Read(buf) if n == 0 && err != nil { // simplified break } fmt.Printf("%s", buf[:n]) // no need to convert to string here } fmt.Println()}
Reference: Https://stackoverflow.com/questions/10152478/how-to-make-a-long-connection-with-http-client