Go Declare Multiple Variables

Efficient ways to declare multiple variables at once

📋 Multiple Variable Declaration

Go allows you to declare multiple variables in a single statement, making your code cleaner and more efficient when working with related data values together.


// Declare multiple variables at once
var name, city string = "Alice", "Boston"
var age, score int = 25, 95
fmt.Println(name, city, age, score)
                                    

Output:

Alice Boston 25 95

Multiple Declaration Methods

🔗

Same Type

Multiple variables of same type

var a, b, c int
🎭

Mixed Types

Variables with different types

var name, age = "John", 30
📦

Block Declaration

Group declarations in blocks

var (
    x int
    y string
)
âš¡

Short Multiple

Short syntax for multiple vars

x, y := 10, "hello"

🔹 Same Type Multiple Declaration

Declare multiple variables of the same type:

// Multiple variables of same type
var firstName, lastName, middleName string
var x, y, z int
var price1, price2, price3 float64

// Assign values
firstName, lastName, middleName = "John", "Doe", "Michael"
x, y, z = 10, 20, 30
price1, price2, price3 = 19.99, 29.99, 39.99

fmt.Printf("Name: %s %s %s\n", firstName, middleName, lastName)
fmt.Printf("Coordinates: (%d, %d, %d)\n", x, y, z)
fmt.Printf("Prices: $%.2f, $%.2f, $%.2f\n", price1, price2, price3)

Output:

Name: John Michael Doe

Coordinates: (10, 20, 30)

Prices: $19.99, $29.99, $39.99

🔹 Mixed Type Multiple Declaration

Declare multiple variables with different types:

// Mixed types with values
var studentName, grade = "Alice", 'A'
var age, gpa = 20, 3.85
var isEnrolled, hasScholarship = true, false

fmt.Printf("Student: %s\n", studentName)
fmt.Printf("Grade: %c, Age: %d\n", grade, age)
fmt.Printf("GPA: %.2f\n", gpa)
fmt.Printf("Enrolled: %t, Scholarship: %t\n", isEnrolled, hasScholarship)

Output:

Student: Alice

Grade: A, Age: 20

GPA: 3.85

Enrolled: true, Scholarship: false

🔹 Block Declaration

Group multiple variable declarations in a block:

// Block declaration for better organization
var (
    companyName   string  = "TechCorp"
    employeeCount int     = 150
    revenue       float64 = 2500000.50
    isPublic      bool    = true
    founded       int     = 2010
)

fmt.Printf("Company: %s\n", companyName)
fmt.Printf("Employees: %d\n", employeeCount)
fmt.Printf("Revenue: $%.2f\n", revenue)
fmt.Printf("Public: %t, Founded: %d\n", isPublic, founded)

Output:

Company: TechCorp

Employees: 150

Revenue: $2500000.50

Public: true, Founded: 2010

🔹 Short Multiple Declaration

Use short syntax for multiple variables (inside functions):

func main() {
    // Short multiple declaration
    productName, price := "Smartphone", 699.99
    quantity, inStock := 25, true
    category, brand := "Electronics", "TechBrand"
    
    fmt.Printf("Product: %s by %s\n", productName, brand)
    fmt.Printf("Category: %s, Price: $%.2f\n", category, price)
    fmt.Printf("Stock: %d units, Available: %t\n", quantity, inStock)
    
    // Multiple assignment to existing variables
    productName, price = "Tablet", 399.99
    fmt.Printf("Updated: %s - $%.2f\n", productName, price)
}

Output:

Product: Smartphone by TechBrand

Category: Electronics, Price: $699.99

Stock: 25 units, Available: true

Updated: Tablet - $399.99

🧠 Test Your Knowledge

Which syntax is used for block variable declaration in Go?