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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历