Go ‘:=’ vs ‘=’
3 min readAug 17, 2020
In Go “:=” is introduced in this language. Detail can be found in [1] and [2]. What is the difference between the “:=” and “=”? Let’s learn through example.
Example code
code
// Go program to illustrate
// concept of variable
package main
import "fmt"func main() {// Using short variable declaration
// Here, short variable declaration acts
// as an assignment for myvar2 variable
// because same variable present in the same block
// so the value of myvar2 is changed from 45 to 100
myvar1, myvar2 := 39, 45
myvar3, myvar2 := 45, 100// If you try to run the commented lines,
// then compiler will gives error because
// these variables are already defined
// myvar1, myvar2 := 43, 47
// myvar2:= 200// Display the values of the variables
fmt.Printf("The value of myvar1 and myvar2 is : %d %d\n",
myvar1, myvar2)
fmt.Printf("The value of myvar3 and myvar2 is : %d %d\n",
myvar3, myvar2)
}
output
The value of myvar1 and myvar2 is : 39 100The value of myvar3 and myvar2 is : 45 100
What will happen if we change “:=” to “=”
// Go program to illustrate
// concept of variable
package main
import "fmt"func main() {// Using short variable declaration
// Here, short variable declaration acts
// as an assignment for myvar2 variable
// because same variable present in the same block
// so the value of myvar2 is changed from 45 to 100
myvar1, myvar2 = 39, 45
myvar3, myvar2 = 45, 100// If you try to run the commented lines,
// then compiler will gives error because
// these variables are already defined
// myvar1, myvar2 := 43, 47
// myvar2:= 200// Display the values of the variables
fmt.Printf("The value of myvar1 and myvar2 is : %d %d\n",
myvar1, myvar2)
fmt.Printf("The value of myvar3 and myvar2 is : %d %d\n",
myvar3, myvar2)
}
Output
# command-line-arguments./use_variable.go:13:1: undefined: myvar1./use_variable.go:13:9: undefined: myvar2./use_variable.go:14:1: undefined: myvar3./use_variable.go:14:9: undefined: myvar2./use_variable.go:24:11: undefined: myvar1./use_variable.go:24:19: undefined: myvar2./use_variable.go:26:11: undefined: myvar3./use_variable.go:26:19: undefined: myvar2
What will happen if we define the variable by keeping on using “=”
// Go program to illustrate
// concept of variable
package main
import "fmt"func main() {// Using short variable declaration
// Here, short variable declaration acts
// as an assignment for myvar2 variable
// because same variable present in the same block
// so the value of myvar2 is changed from 45 to 100
var myvar1, myvar2 = 39, 45
var myvar3, myvar2 = 45, 100// If you try to run the commented lines,
// then compiler will gives error because
// these variables are already defined
// myvar1, myvar2 := 43, 47
// myvar2:= 200// Display the values of the variables
fmt.Printf("The value of myvar1 and myvar2 is : %d %d\n",
myvar1, myvar2)
fmt.Printf("The value of myvar3 and myvar2 is : %d %d\n",
myvar3, myvar2)
}
Output
# command-line-arguments./use_variable.go:14:13: myvar2 redeclared in this blockprevious declaration at ./use_variable.go:13:5
We can not declare a same variable more than once as shown HERE
The workable code
code
// Go program to illustrate
// concept of variable
package main
import "fmt"func main() {// Using short variable declaration
// Here, short variable declaration acts
// as an assignment for myvar2 variable
// because same variable present in the same block
// so the value of myvar2 is changed from 45 to 100
var myvar1 = 39
var myvar3, myvar2 = 45, 100// If you try to run the commented lines,
// then compiler will gives error because
// these variables are already defined
// myvar1, myvar2 := 43, 47
// myvar2:= 200// Display the values of the variables
fmt.Printf("The value of myvar1 and myvar2 is : %d %d\n",
myvar1, myvar2)
fmt.Printf("The value of myvar3 and myvar2 is : %d %d\n",
myvar3, myvar2)
}
Output
The value of myvar1 and myvar2 is : 39 100The value of myvar3 and myvar2 is : 45 100
Attentions [1]
With the help of short variable declaration operator(:=) you can only declare the local variable which has only block-level scope. Generally, local variables are declared inside the function block. If you will try to declare the global variables using the short declaration operator then you will get an error.
Reference
[1]https://www.geeksforgeeks.org/short-variable-declaration-operator-in-go/