go语言ioutil.ReadAll()报错unexpected EOF

 如果你比较急那么解决问题之后再看这段话,建议看一下:

  When Read encounters an error or EOF (end-of-file) after successfully reading n > 0 bytes, it returns the number of bytes read.
  In general, Reader will return a non-zero byte number n, if n = len(p) bytes are returned by Read from the end of the input source.
  Read may return err == EOF or err == nil. And then Read() should return (n:0, err:EOF).The caller should process the returned data first before considering the error. Doing so correctly handles I/O errors that occur after reading some bytes, while allowing EOF to occur.

该问题并非不正常,出现时大多能正常返回数据,处理方式:

...省略部分代码...
response, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		if strings.Contains(err.Error(), "unexpected EOF") && len(response) != 0 {
			logrus.Printf("when read response: %s, will parse to YAML's []byte.", err.Error())
			goto pars
		}
		return nil, err
	}
pars:
//执行你的下一步操作即可
	bytes, err := yaml.JSONToYAML(response)
	return bytes, err

即,以合适的方式取你的数据即可。

你可能感兴趣的:(go,golang)