no new variables on left side of :=

前端 未结 4 604
深忆病人
深忆病人 2020-12-13 08:18

What\'s happening here?

package main

import \"fmt\"

func main() {

    myArray  :=[...]int{12,14,26}  ;     
    fmt.Println(myArray)

    myArray  :=[...         


        
4条回答
  •  臣服心动
    2020-12-13 09:06

    There are two types of assignment operators in go := and =. They are semantically equivalent (with respect to assignment) but the first one is also a "short variable declaration" ( http://golang.org/ref/spec#Short_variable_declarations ) which means that in the left we need to have at least a new variable declaration for it to be correct.

    You can change the second to a simple assignment statement := -> = or you can use a new variable if that's ok with your algorithm.

提交回复
热议问题