(Go Lang) Variables (GBEN)

Go by Example

Example

  • Typed variables are either declared explicitly or inferred
  • Assignment is =
  • Both multiple declaration and assignment
  • Numeric initialize default to zero
  • := → declaration of, inference and assignment to

// var declares 1 or more variables.
var a string = "initial"
fmt.Println(a)
// You can declare multiple variables at once.
var b, c int = 1, 2
fmt.Println(b, c)
// Go will infer the type of initialized variables.
var d = true
fmt.Println(d)
// Variables declared without a corresponding initialization are zero-valued. For example, the zero value for an int is 0.
var e int
fmt.Println(e)
// The := syntax is shorthand for declaring and initializing a variable, e.g. for var f string = "short" in this case.
f := "short"
fmt.Println(f)
initial
1 2
true
0
short

Attribution


Leave a Reply

Your email address will not be published. Required fields are marked *