BudiBadu Logo

Samplebadu

Code with Example
BudiBadu Logo
Samplebadu

Go by Example: Variables

Go 1.23

Learn the various ways to declare and initialize variables in Go. This example covers the standard `var` keyword, type inference, multiple variable declarations, zero values, and the concise short declaration syntax `:=`, providing a complete overview of variable management in Go.

Code

package main

import "fmt"

func main() {
    // var declares 1 or more variables
    var a = "initial"
    fmt.Println(a)

    // Declare multiple variables at once
    var b, c int = 1, 2
    fmt.Println(b, c)

    // Go infers the type automatically
    var d = true
    fmt.Println(d)

    // Variables without initialization get zero values
    var e int
    fmt.Println(e)

    // := is shorthand for declare and initialize
    f := "apple"
    fmt.Println(f)
}

Explanation

Go offers two primary ways to declare variables: the explicit var keyword and the short declaration operator :=. The var keyword provides clear type specification and works at both package and function levels, while := offers concise syntax for local variables with type inference.

Variables declared with var without explicit initialization automatically receive zero values—a critical safety feature that eliminates the entire class of "uninitialized variable" bugs common in languages like C and C++. This guarantees that every variable has a well-defined value before use. Zero values are: 0 for numeric types, false for booleans, "" (empty string) for strings, and nil for pointers, slices, maps, channels, functions, and interfaces.

The short declaration operator := combines declaration and initialization in a single, concise statement. It's the preferred method for local variables inside functions when the type is obvious from the right-hand side. However, := can only be used inside functions—package-level variables must use var. Multiple variables can be declared and assigned simultaneously using either syntax.

Best practices for variable declaration:

  • := for local variables when type is clear from context
  • var with explicit type annotation when clarity trumps conciseness
  • var at package level since := isn't allowed there
  • Multiple declaration (a, b := 1, 2) for related variables
  • Rely on zero values instead of explicit initialization to default values

Code Breakdown

7-8
Declares variable "a" using var with type inference. Go sees "initial" is a string literal, so it infers that "a" should be type string. This is equivalent to "var a string = 'initial'" but more concise.
11-12
Demonstrates multiple variable declaration in a single line. Both b and c are declared as int type and initialized to 1 and 2 respectively. This syntax is useful when you have related variables of the same type.
15-16
Another example of type inference with var. The value "true" is a boolean literal, so Go infers that "d" is type bool. Type inference is particularly useful for complex types where writing out the full type would be verbose.
19-20
Declares an int variable "e" without initialization. In many languages, this would leave "e" with a garbage value, but Go automatically initializes it to the zero value for int, which is 0. This is a key safety feature of Go.
23-24
The short declaration operator := combines declaration and initialization. This is the most common way to declare local variables in Go. The type is inferred from the right-hand side. Important: := can only be used inside functions, not at package level.