Home  >  Article  >  Backend Development  >  How to initialize multiple variables in go language

How to initialize multiple variables in go language

青灯夜游
青灯夜游Original
2023-01-13 11:37:413792browse

Methods for initializing multiple variables: 1. Use the var keyword to define and initialize multiple variables at one time, the syntax "var(variable 1 = expression 1 variable 2 = expression 2 variable 3 = expression Formula 3)"; 2. Use the colon equal to ":=" to define and initialize multiple variables at one time, the syntax is "variable 1, variable 2, variable 3, ... := expression 1, expression 2, expression Formula 3,...".

How to initialize multiple variables in go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Go language variables can support defining and initializing multiple variables at the same time when defining them. Go language supports the use of var syntax or := syntax when declaring and initializing multiple variables.

Method 1: Use var statement for multi-variable initialization

We can use the var keyword to define and initialize multiple variables at once .

Syntax:

var(
    变量1 = 表达式1
    变量2 = 表达式2
    变量3 = 表达式3
)

Example: Use the form of var() to define and initialize multiple variables at the same time

package main
import "fmt"
func main() {
	var(
		name = "hello"
		site = "world"
		isOnline = true
	)
	fmt.Println("Name =", name, ",Site =", site, ",IsOnline =", isOnline)
}

How to initialize multiple variables in go language

Method 2: Use a colon equal to "<span style="font-size: 18px;">:=</span>" for multi-variable initialization

We can use the form := to define and initialize multiple variables at one time.

Syntax:

变量1, 变量2, 变量3, ... := 表达式1, 表达式2, 表达式3, ...

Example: Use the form := to define and initialize multiple variables at the same time

package main
import "fmt"
func main() {
	name, site, isOnline := "hi", "hello world", true
    fmt.Println("Name =", name, "\nSite =", site, "\nIsOnline =", isOnline)
}

How to initialize multiple variables in go language

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of How to initialize multiple variables in go language. 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