当我编写一个返回带有延迟的错误的方法时,是不可能的。 IDE linter等会警告它。
为了避免这种情况,我编写了以下代码。
package main
import (
"io/ioutil"
"log"
"net/http"
)
func main() {
req,_ := http.NewRequest("GET","https://example.com",nil)
cli := &http.Client{}
res,_ := cli.Do(req)
defer func() {
_ = res.Body.Close() // catch the error
}()
b,_ := ioutil.ReadAll(res.Body)
log.Println(string(b))
}