When is the comma-separated variable declaration method typically used in Go?

092

In Go, the comma-separated variable declaration method is typically used when you want to declare multiple variables of the same type in a single statement. This method is particularly useful for improving code readability and conciseness.

Here’s an example:

package main

import "fmt"

func main() {
    var a, b, c int
    a, b, c = 1, 2, 3
    fmt.Println(a, b, c)
}

In this example, a, b, and c are declared as integers in a single line, making the code cleaner and easier to read. This method can also be used with short variable declarations using the := syntax:

func main() {
    x, y, z := 10, 20, 30
    fmt.Println(x, y, z)
}

This approach is commonly used when initializing multiple variables at once.

0 Comments

no data
Be the first to share your comment!