Gaaker Go

Golang Variables initialization

Variables can be specified with an initial value when they are declared.

Use the assignment operator (=) to assign a value to the variable. Multiple values are separated by commas and correspond to the variable one-to-one. The format is as follows:

Declare and initialize only one variable.

var name type = value

Declare and initialize multiple variables of the same type

var name1, name2 ... type = value1, value2 ...

Declare and initialize different types of variables at once. Examples are as follows:

var (
    a int = 1
    b string = "1"
    c bool = true

The following is a complete code example.

package main

import "fmt"

var a, b, c int = 1, 2, 3

func main() {
	var d string = "string"
	var e, f bool = true, false

	fmt.Println(a, b, c, d, e, f)

Below is the program output:

1 2 3 string true false
< Variables initialization Variables initialization >