Home >Backend Development >Golang >How Can I Declare Multiple Variables in Go?

How Can I Declare Multiple Variables in Go?

DDD
DDDOriginal
2025-01-01 10:59:09970browse

How Can I Declare Multiple Variables in Go?

Multiple Variable Declarations in Go

Go provides a way to declare multiple variables at once, simplifying code and improving efficiency.

Inline Assignment:

To declare multiple variables and assign them values inline, use the following syntax:

a, b, c := 80, 80, 80

This statement declares three variables (a, b, and c) and assigns each the value 80.

Type Declaration:

To declare multiple variables without assigning values, use the var keyword:

var a, b, c string

This statement declares three variables (a, b, and c) as strings without assigning any values.

Example:

The following code demonstrates multiple variable declarations:

package main

import "fmt"

func main() {
    // Inline assignment
    a, b, c := 80, 80, 80

    // Type declaration
    var x, y, z string
    x = "foo"

    fmt.Println(a, b, c, x)
}

Output:

80 80 80 foo

The above is the detailed content of How Can I Declare Multiple Variables in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn