Golang Variables And Inferred Typing
In Golang the var
statement declared a list of variables with the type declared last.
var(
name string
age int
location string
)
Or even
var(
name, location string
age int
)
Variables can also be declared one by one
var name string
var age int
var location string
A var declaration can include initializers, one per variable
var(
name string = "Mayra"
age int = 27
location string = "MTY"
)
If an initializer is present the type can be omitted, the variable will take the type of the initializer (inferred typing)
var(
name = "Mayra"
age = 27
location = "MTY"
)
You can also initialize variables on the same line:
var(
name, location, age = "Mayra", 27, "MTY"
)
Inside a function, the :=
short assignment statement can be used in place of var declaration with implicit type
func main(){
name, location := "Mayra", "MTY"
age := 32
fmt.Printf("%s (%d) of %s", name, age, location)