In this code, we witness an issue in the second statement, resulting in an error message "no new variables on left side of :=":
package main import "fmt" func main() { myArray :=[...]int{12,14,26} // Correct: Short declaration with assignment using ":" fmt.Println(myArray) myArray :=[...]int{11,12,14} // Error: Second assignment with ":" attempts to create a new variable fmt.Println(myArray) ; }
To address this issue, it is crucial to comprehend that the colon symbol (:) is specifically employed during the initial declaration and assignment of a variable. In this case, the first statement is legitimate:
myArray :=[...]int{12,14,26} // Declaring and assigning an array with ":"
However, when re-assigning values to an existing variable, as attempted in the second statement, the colon should be removed:
myArray = [...]int{11,12,14} // Re-assignment without ":"
In summary, remember to utilize the colon (:) only during the initial declaration and assignment of a variable. For subsequent re-assignments, rely on the equal sign (=). This modification would rectify the code and resolve the error.
以上がGo で「:= の左側に新しい変数がありません」というエラーが表示されるのはなぜですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。