golang的http关闭问题

 测试版本go1.4.2

	resp, err := http.Get("http://example.com/")
	if resp != nil {
		defer resp.Body.Close()
	}

	if err != nil {
		fmt.Println("err: ", err)
		return
	}

	_, err = ioutil.ReadAll(resp.Body)  //去掉这句,则主动关闭连接 ???
这里看到的解释: The orignal implementation for resp.Body.Close() also reads and discards the remaining response body data. This ensured that the http connection could be reused for another request if the keepalive http connection behavior is enabled. The latest http client behavior is different. Now it's your responsibility to read and discard the remaining response data. If you don't do it the http connection might be closed instead of being reused. This little gotcha is supposed to be documented in Go 1.5.

猜你喜欢

转载自simplehappy.iteye.com/blog/2223760