go - 如果 block 具有多于两行(wsl),则不应拥抱return语句

标签 go

我的lint返回了此错误消息,但我真的不明白这是什么意思。

如标题所示:
return statements should not be cuddled if block has more than two lines (wsl)
我的代码是这样的:

func validateCountry(product models.Product, countries []models.Country) bool {
    if !product.CountryCode.Valid {
        return true
    }

    for _, country := range countries {
        if country.Code == product.CountryCode.String {
            return !country.Enabled && country.Deprecated
        }
    }

    return false
}

短绒棉布不喜欢的似乎是最后的return false

我很困惑,我没有在此代码库中设置lint,而且我真的不知道如何跳过此规则或如何解决它。

最佳答案

此错误意味着,就您而言,您需要在任何下一个return语句之前放置一个空行:

[empty line] <-- need this
return true
...
[empty line] <-- need this
return !country.Enabled && country.Deprecated
should not be cuddled表示return语句附近必须没有代码行。

关于go - 如果 block 具有多于两行(wsl),则不应拥抱return语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58328069/

相关文章:

regex - golang 如何使用正则表达式将字符串拆分为包含分隔符的 slice

go - 生产中的 vue-router(使用 Go 服务)

go - 在 Iris Framework 的 POST 方法中重定向

http - 从 Golang 中的网络/上下文中获取值

parsing - 为什么 time.Parse 不使用时区?

go - 如何在更改位置时更新导入

parsing - 是否有类似于 Python 构造的 golang 库?

postgresql - "runtime error: invalid memory address or nil pointer dereference"创建表

go - 如何在 terratest 中传递 assume 角色

string - 如何查看映射键是否包含某些字符串?