go语言 ioutil.ReadAll 正确使用方法,ioutilReadAll 限流,限制读取大小

req, err := http.NewRequest(c.method, c.url, reader)
		if err != nil {
			return nil, err
		}
		req.Header = c.Header
		req.Header.Set("Content-Type", c.ContentType)
		c.HttpClient.Transport = c.Transport
		return req, nil
	}()
	if err != nil {
		c.Errors = append(c.Errors, err)
		return nil, nil, c.Errors
	}
	if c.Timeout != time.Duration(0) {
		c.HttpClient.Timeout = c.Timeout
	}
	if c.Host != "" {
		req.Host = c.Host
	}
	res, err = c.HttpClient.Do(req)
	if err != nil {
		c.Errors = append(c.Errors, err)
		return nil, nil, c.Errors
	}
	defer res.Body.Close()

      //默认 3MB 可以改成你自己想要的 bs, err = ioutil.ReadAll(io.LimitReader(res.Body, int64(3<<20)))// default 3MB change the size you want
if err != nil { c.Errors = append(c.Errors, err) return nil, nil, c.Errors }

 

posted @ 2020-12-24 19:59  石门  阅读(3454)  评论(0编辑  收藏  举报