GoLang – Error – “no new variables on left side of :=”

Error

Error Image

Error Text


no new variables on left side of :=

Source Code

Original


package main

import (
    "fmt"
)


func variableDeclaration(){

    /*
        Declare Variables
    */
    var strBuffer string
    
    strBuffer := "";
    
    fmt.Println(strBuffer);

}


func main(){
    variableDeclaration()
}

 

Revision

Outline

  1. The original code read
    • strBuffer := “Sam”
  2. The := means to declare a new variable and set its value
    • And, so strBuffer := “Sam” reads
      • Declare a new variable ( strBuffer )
      • Set the variable value to Sam
  3. Please rewrite as
    • strBuffer = ‘Sam”

Code

package main

import (
    "fmt"
)


func variableDeclaration(){

    /*
        Declare Variables
    */
    var strBuffer string
    
    //No need for :
    //Simply replace := with =
    //strBuffer := "Sam";
    strBuffer = "Sam";
    
    fmt.Println(strBuffer);

}


func main(){
    variableDeclaration()
}

 

Source Code Control

Github

  1. Repository
    • Repository – Folder – Golang/exercises/error/NoNewVariables
      Link

      • Files
        • NoNewVariables.go
          Link
        • NoNewVariablesRevised.go
          Link

Summary

Only Daniel can make a rookie mistake on a HelloWorld sample code.

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s