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

How Can I Declare Multiple Variables in Go?

DDD
Release: 2025-01-01 10:59:09
Original
969 people have browsed it

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
Copy after login

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
Copy after login

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)
}
Copy after login

Output:

80 80 80 foo
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template