已声明且未在条件语句中使用的变量 [英] Variable declared and not used in conditional statement
问题描述
我在条件语句之外声明了一些变量( offsetI
和 limitI
).在条件语句内部,我尝试为它们分配值,然后在条件语句之后将这些值用于查询.
I declare some variables (offsetI
and limitI
) outside of a conditional statement. Inside the conditional statement I am trying to assign them values, then use those values for a query after the conditional statement.
var (
number, size, offset, limit string
offsetI, limitI uint64
)
// Get the string values for number, size, offset, and limit
// ...
if size != "" {
// Parse the number value
numberI, err := strconv.ParseUint(number, 10, 64)
if err != nil {...}
// Parse the size value
limitI, err = strconv.ParseUint(size, 10, 64)
if err != nil {...}
// Calculate the offset
offsetI = numberI * limitI
} else {
// Parse the limit value
limitI, err := strconv.ParseUint(limit, 10, 64) // limitI declared and not used
if err != nil {...}
// Parse the offset value
offsetI, err = strconv.ParseUint(offset, 10, 64)
if err != nil {...}
}
// Make the query using offsetI and limitI
result, err := s.GetAllPaginated(offsetI, limitI)
if err != nil {...}
我不打算在 else
语句的范围内重新声明 limitI
变量,但是我需要使用:=
运算符,用于声明新的 err
变量.
I am not intending to re-declare the limitI
variable in the scope of the else
statement, but I need to use the :=
operator for declaring a new err
variable.
我唯一能想到的就是分别声明另一个 err
变量,因此我可以使用常规的赋值语句:
The only thing I could come up with was to separately declare another err
variable, so I could use a regular assignment statement:
} else {
var err error // New
// Regular assignment statement now
limitI, err = strconv.ParseUint(limit, 10, 64)
if err != nil {...}
我希望能够不必声明其他错误变量就能做到这一点.
I would like to be able to do this without having to declare an additional error variable.
推荐答案
额外的 var错误
很尴尬,但这是解决这种情况的常用方法.范围界定规范说(强调我):
The extra var error
is awkward, but it's a common way to address this situation. The spec on scoping says (emphasis mine):
在函数内部声明的常量或变量标识符的范围始于ConstSpec或VarSpec的末尾(对于简短变量声明,为ShortVarDecl),并终止于最内层的包含块的末尾./p>
因此,在您的情况下,简短的变量声明是声明一个 different limitI
范围为最内层的块".由于它只能生存"到下一个大括号,因此不会使用.
So in your case, that short variable declaration is declaring a different limitI
scoped to the "innermost containing block." Since it only "lives" until the next closing brace, it isn't used.
在您的特定情况下,一个选项可能是在if/else之外声明 err
,因为这两个内部作用域都使用了它,因此您可以改用 =
:=
的代码,这些函数返回 error
s.那么就没有声明内部 limitI
",并且您没有未使用的变量问题.
In your specific case, an option might be to declare err
outside the if/else, since it's used in both inner scopes, so you can use use =
instead of :=
with those functions returning error
s. Then there's no "inner limitI
" declared and you have no unused variable issue.
像这样的阴影"情况也会产生意外的行为,而不是错误. go vet -shadow
尝试检测"[v]可能包含被无意间遮盖了" ,并且与此不同但又相关, gordonklaus/ineffasign 概括了未使用的变量"检查以检测无用的分配,即使它们不是声明.
"Shadowing" situations like this can also produce unexpected behavior rather than an error. go vet -shadow
tries to detect "[v]ariables that may have been unintentionally shadowed" and, different but related, gordonklaus/ineffasign generalizes the "unused variable" check to detect useless assignments even if they weren't declarations.
这篇关于已声明且未在条件语句中使用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!