Sling Academy
Home/Golang/Grouped and shorthand declaration in Go

Grouped and shorthand declaration in Go

Last updated: November 23, 2024

In the Go programming language, variable declaration can be executed using both grouped and shorthand notations, offering flexibility and readability to the code. This article will guide you through these concepts with examples ranging from basic to advanced implementations.

Basic Variable Declaration

Before exploring grouped and shorthand declarations, it's essential to understand the standard variable declaration in Go. Variables in Go are explicitly declared using the var keyword which defines the name and the type of variable.

package main

import "fmt"

func main() {
    var name string
    name = "Gopher"
    fmt.Println("Name:", name)
}

In this example, we declared a name variable of type string and assigned it a value.

Grouped Declaration

Go allows declaring multiple variables at once using grouped declarations, improving the neatness of the code significantly.

package main

import "fmt"

func main() {
    var (
        firstName string
        lastName  string
        age       int
    )
    
    firstName = "John"
    lastName = "Doe"
    age = 30
    fmt.Printf("Name: %s %s, Age: %d\n", firstName, lastName, age)
}

Here, the grouping of variable declarations using parentheses allows you to list multiple variables at the same time.

Shorthand Declaration

The shorthand declaration is a unique feature in Go, which uses the := operator for both declaring and initializing variables. This syntax eliminates the need for the var keyword and works only within functions.

package main

import "fmt"

func main() {
    firstName := "Jane"
    lastName := "Doe"
    age := 28
    fmt.Printf("Name: %s %s, Age: %d\n", firstName, lastName, age)
}

The shorthand declaration is a concise way to create and initialize variable values in one step.

Advanced Usage: Combined Grouped and Shorthand Declarations

In more advanced scenarios, Go allows combining both grouped and shorthand declarations within functions.

package main

import "fmt"

func main() {
    var (
        country, city = "USA", "New York"
        population int
    )
    population = 8419000
    fmt.Printf("%s, %s, Population: %d\n", city, country, population)
}

In this example, the code demonstrates a combination of grouping for declare-assign expressions with explicit assignment of another variable underneath.

With these techniques, Go programmers can manage their variable declarations efficiently, improving both clarity and maintainability of the code. Adopting grouped and shorthand declaration practices renders the code neater and often more readable.

Next Article: Using For loop in Go (from basic to advanced)

Previous Article: Working with constants in Go

Series: Variables & Control Flow

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant