Go has two ways to declare a variable: the var keyword and the short form :=. With var you can state the type, let the compiler infer it from the value, or leave the value out. A variable declared without a value starts at its type's zero value.
The short variable declaration name := "John" always needs a value, infers the type from it, and works only inside functions. Both forms can declare several variables on one line. The compiler rejects a local variable that is declared but never used.
// Explicit type declaration
var name string = "John"
var age int = 30
// Type inference
var user = "John"
var score = 30
// Short variable declaration (inside functions only)
nick := "John"
level := 30
// Multiple variable declaration
var a, b, c int = 1, 2, 3
x, y := 10, "hello"
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓