请问golang中如何处理这种场景的异常?
package requests
请问golang中如何处理这种场景的异常?
可以看到有两个err 的处理没有实现 当用户输入错误的url 比如网址不存在 时候会直接panic
func Request(params *Params, config *config.Config) interface{} { // 这里的接收 必须使用 return response.Response(body, params.Method, err)
paramsJson, _ := json.Marshal(params)
req, err := http.NewRequest("POST", config.Url, bytes.NewBuffer(paramsJson))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
fmt.Println(err.Error())
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// 处理响应 将err传入到这个方法中有其他逻辑处理
return response.Response(body, params.Method, err)
}
方式一:
方式二: