本文介绍了在:=的左侧没有新的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里发生了什么?








$ b func main() {

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

myArray:= [...] int {11,12,14} //错误指向此行

fmt .Println(myArray);


它引发一个错误,说

 左侧没有新变量:= 

我正在做的是将值重新分配给已经声明的变量。

解决方案

c $ c>冒号:,因为您正在为现有变量指定一个新值。

  myArray = [...] int {11,12,14} 

<$ c $当你在你的第一个语句中进行第一次执行短的声明和赋值时,使用c>冒号:,例如 myArray:= [...] int { 12,14,26} 。


What's happening here?

package main

import "fmt"

func main() {

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

    myArray  :=[...]int{11,12,14} //error pointing on this line 

    fmt.Println(myArray) ;

}

It throws an error that says

no new variables on left side of :=

What I was doing was re-assigning values to an already declared variable.

解决方案

Remove the colon : from the second statement as you are assigning a new value to existing variable.

myArray = [...]int{11,12,14}

colon : is used when you perform the short declaration and assignment for the first time as you are doing in your first statement i.e. myArray :=[...]int{12,14,26}.

这篇关于在:=的左侧没有新的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:08