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 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'