golang: if block ends with a return statement, so drop this else and outdent its block

golang中有一条代码规范检查: if block ends with a return statement, so drop this else and outdent its block

例如以下代码: else处会提示: if block ends with a return statement, so drop this else and outdent its blockgo-lint

if k8serrors.IsNotFound(err) {
    c.deletePodQueue.Add(key)
    return
} else {
    klog.Errorf("failed to get statefulset %v", err)
    return
}

网上搜索有部分不太正确的解释或者修改方式.

正确原因是:
代码进了if之后, 会直接return. 不会进else
如果代码没有进if, 自然进下面的else代码.所以else多余.
修改为以下:

if k8serrors.IsNotFound(err) {
    c.deletePodQueue.Add(key)
    return
}

klog.Errorf("failed to get statefulset %v", err)
return

你可能感兴趣的:(golang: if block ends with a return statement, so drop this else and outdent its block)