Gaaker Go

Golang Short variable declarations

In a function, the short variable declaration statement := can replace the var declaration, and the format is as follows:

A variable

name := value

Many variables

name1, name2 ... := value1, value2 ...

Its syntax is very concise, realize variable declaration and assignment quickly. However, the short variable declaration statement can only be used within a function.

The following is a complete code example.

package main

import "fmt"

func main() {
	tomAge, jackAge := 17, 22
    isAdult := true
	fmt.Println(tomAge, jackAge, isAdult)
}

Below is the program output:

17 22 true
< Short variable declarations Short variable declarations >