1.5 KiB
title |
---|
Go Variables |
Variable declarations in Go
Method 1: Regular Variable Declarations
A regular variable declaration creates one or more variables by binding identifiers with a type and an initial value. If a variable is declared without a type, then that variable is given the type of the corresponding initialization value in the assignment. If a variable is defined with no initial value, then the variable is initialized to its zero value.
The following examples are all valid variable declarations in go:
var x int = 1
var y int
var z = 0
var a, b float32 = -1, -2
Method 2: Short Variable Declarations
Shorthand variable declarations create variables with only an identifier and an initial value. The var
keyword and types are not needed to declare a variable using shorthand syntax:
x := 1
text, err := ioutil.ReadAll(reader)
Short variable declarations may appear only inside functions. In some contexts such as the initializers for if
, for
, or switch
statements, they can be used to declare local temporary variables.