Gaaker Go

Golang Variables

var is used to declare one or more variables, multiple variables are separated by commas, and the type of the variable is at the end. As shown in the structure below.

var name type

var name 1, name 2 ... type

You can also declare multiple different types of variables at once.

var (
    a int
    b string
    c float32

Variables can be declared in packages and functions, and the scope between them is different.

The following is a complete code example.

package main

import "fmt"

var a, b, c int

func main() {
	var d string
    var e, f bool
	fmt.Println(a, b, c, d, e, f)

Below is the program output:

0 0 0  false false
< Variables Variables >