doulanyan6455 2015-01-13 09:57
浏览 148
已采纳

为什么在Go中有两种声明变量的方法,有什么区别以及使用哪种方法?

According to the Go reference there are two ways of declaring a variable

Variable_declarations (in the format of var count = 0 or var count int)
and
Short_variable_declarations (in the format of count := 0)

I found it's very confusing to decide which one to use.

The differences I know (till now) are that:

  • I can only using a count := 0 format when in the scope of a function.
  • count := 0 can be redeclared in a multi-variable short declaration.

But they do behave the same as far as I know. And in the reference it also says:

It (the count:=0way) is shorthand for a regular variable declaration with initializer expressions but no types

My confusions are:

  • If one is just the shorthand way of the other, why do they behave differently?
  • In what concern does the author of Go make two ways of declaring a variable (why are they not merged into one way)? Just to confuse us?
  • Is there any other aspect that I should keep my eyes open on when using them, in case I fall into a pit?
  • 写回答

1条回答 默认 最新

  • dongtan8532 2015-01-13 10:23
    关注

    The Variable declarations make it clear that variables are declared. The var keyword is required, it is short and expresses what is done (at the file level everything excluding comments has to start with a keyword, e.g. package, import, const, type, var, func). Like any other block, variable declarations can be grouped like this:

    var (
        count int
        sum   float64
    )
    

    You can't do that with Short variable declarations. Also you can use Variable declarations without specifying the initial value in which case each variable will have the zero value of its type. The Short variable declaration does not allow this, you have to specify the initial value.

    One of Go's guiding design principle was to make the syntax clean. Many statements require or it is handy that they allow declaring local variables which will be only available in the body of the statement such as for, if, switch etc. To make the syntax cleaner and shorter, Short variable declaration is justified in these cases and it is unambigous what they do.

    for idx, value := range array {
        // Do something with index and value
    }
    
    if num := runtime.NumCPU(); num > 1 {
        fmt.Println("Multicore CPU, cores:", num)
    }
    

    Another difference: Redeclaration

    Quoting from the Language specification:

    Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.

    This one is also handy. Suppose you want to do proper error handling, you can reuse an err variable because most likely you only need it to check if there were any errors during the last function call:

    var name = "myfile.txt"
    
    fi, err := os.Stat(name) // fi and err both first declared
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(name, fi.Size(), "bytes")
    
    data, err := ioutil.ReadFile(name) // data is new but err already exists
                                       // so just a new value is assigned to err
    if err != nil {
        log.Fatal(err)
    }
    
    // Do something with data
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集