Declare variable and assign value golang tutorial 3
Declare variable and assign value golang tutorial 3
In last tutorial we have learnt how to declare variable.
write var keyword , followed by variable name and then variable type
var a int[code]
To assign a value to variable. It is very easy. just put equal sign and write your value.
[code] var a int = 420 [code]
or simply you can do like this if you have already declared variable.
[code] a = 420 [code]
another shortcut to declare varialbe and assign value is
[code] var a = 420 [code]
It will assume the type of variable.
Lets unveil another very very shortcut way to declare a variable.
[code] a := 420 [code]
Just variable name , colon , equal sign and variable value.
<strong>Source Code of normal way to declare variable.</strong>
[code]
package main
import "fmt"
func main(){
var a int = 33
fmt.Print(a);
}
Source Code of shortcut way to declare variable.
package main
import "fmt"
func main(){
var b = 343
fmt.Print(b);}
Source Code of shortest way to declare variable.
package main
import "fmt"
func main(){
c:=333
fmt.Print(c);
}