duaiwo9093 2016-07-03 23:51
浏览 55
已采纳

已声明且未在条件语句中使用的变量

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 {...}

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.

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.

  • 写回答

2条回答 默认 最新

  • droe9376 2016-07-04 00:05
    关注

    The extra var error is awkward, but it's a common way to address this situation. The spec on scoping says (emphasis mine):

    The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.

    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.

    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 errors. Then there's no "inner limitI" declared and you have no unused variable issue.

    "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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度