Golang Tutorial

Fundamentals

Control Statements

Functions & Methods

Structure

Arrays & Slices

String

Pointers

Interfaces

Concurrency

var keyword vs short declaration operator in Golang

In Go, there are multiple ways to declare variables. Two of the most commonly used methods are the var keyword and the short declaration operator (:=). Let's explore the differences and usages of both in this tutorial.

1. var Keyword

The var keyword is a classic way to declare variables in Go. It's versatile, allowing you to specify the type of the variable, or omit it and let Go infer the type.

1.1 Declaring with explicit type:

var x int
var y string = "Hello"

1.2 Type inference:

var a = 42
var b = "Gopher"

In this case, a is inferred to be of type int, and b is inferred to be of type string.

1.3 Declaring multiple variables:

var c, d int = 3, 4
var e, f = "Go", "lang"

2. Short Declaration Operator (:=)

The short declaration operator is a more concise way to declare and initialize variables. It can only be used within a function body.

2.1 Basic usage:

func main() {
    x := 10
    y := "Gopher"
    fmt.Println(x, y)
}

Here, x is inferred to be of type int, and y is inferred to be of type string.

2.2 Declaring multiple variables:

func main() {
    a, b := 20, "Hello"
    fmt.Println(a, b)
}

3. Key Differences

  • Scope:
    • The var keyword can be used for both local and package-level variables.
    • The := operator is restricted to function-level scope.
  • Re-declaration:
    • With the short declaration operator, you can re-declare variables if at least one of the variables in the left-hand side of := is new. This can be useful in multiple assignment scenarios.
    • With the var keyword, re-declaring a variable in the same block will result in a compile-time error.
  • Zero values:
    • Variables declared with the var keyword, without an explicit initial value, are given their zero value (e.g., 0 for numerics, false for booleans, and "" for strings).
    • The short declaration operator requires an initial value.
  • Verbosity:
    • The short declaration operator is more concise, making it preferable for local variable declarations.
    • The var keyword is more verbose but offers more flexibility, especially when declaring package-level variables or when a variable's type needs to be explicitly specified.

Conclusion

Both the var keyword and the short declaration operator have their places in Go code. The choice between them depends on the context and the specific needs of the code segment you're working on. The short declaration operator (:=) offers concise syntax and is excellent for local variables, while the var keyword provides more flexibility and is suitable for various scopes.

  1. When to Use var in Golang:

    • Use var for explicit variable declaration when you need to specify the type or when not using the short declaration syntax.
    • Example:
      package main
      
      import "fmt"
      
      var globalVar int
      
      func main() {
          var localVar string
          fmt.Println(globalVar, localVar)
      }
      
  2. Short Declaration Operator in Golang Explained:

    • The short declaration (:=) is used for concise variable declaration and assignment.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          message := "Hello, Golang!"
          fmt.Println(message)
      }
      
  3. Implicit Type Inference with Short Declaration in Golang:

    • The short declaration infers the variable type from the assigned value.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          age := 25 // Inferred as int
          name := "Alice" // Inferred as string
      
          fmt.Println(age, name)
      }
      
  4. Explicit Type Specification with var in Golang:

    • Use var when you want to explicitly specify the variable type.
    • Example:
      package main
      
      import "fmt"
      
      var explicitVar float64
      
      func main() {
          explicitVar = 3.14
          fmt.Println(explicitVar)
      }
      
  5. Golang Variable Declaration Examples:

    • Examples of variable declarations using var and short declaration.
    • Example:
      package main
      
      import "fmt"
      
      var globalVar int
      
      func main() {
          var localVar int
          localVar = 42
      
          message := "Hello, Golang!"
          fmt.Println(globalVar, localVar, message)
      }
      
  6. Using var vs := for Variable Initialization in Golang:

    • Use var when you need to declare a variable without immediately assigning a value or when specifying the type.
    • Use := for concise variable initialization with implicit type inference.
    • Example:
      package main
      
      import "fmt"
      
      var globalVar int // Declaration with var
      
      func main() {
          localVar := 42 // Short declaration for initialization
          fmt.Println(globalVar, localVar)
      }