在 Go 中,是否可以仅使用函数返回?
例如,我想检查错误,而不是重复 if 语句,我只想调用一个函数,例如checkErrors(err).
这是一个例子(goplayground)
func foobar(str string) err {
_, err := ioutil.ReadAll(resp.Body)
checkError(err) // If error != nil then foobar will return the err
/*if err != nil {
return err
}*/
}
func checkError(err error) {
if err != nil {
// Then make the function this is being called within return the err.
}
}
饮歌长啸
蓝山帝景
相关分类