Go - Variables

Variables come from mathematics and are abstract concepts in computer languages that store computational results or represent values.

Variables can be accessed via variable names.

Go language variable names consist of letters, numbers, and underscores, where the first character cannot be a number.

The general form of declaring variables is to use the var keyword:

var identifier type

You can declare multiple variables at once:

var identifier1, identifier2 type


package main
import "fmt"
func main() {
    var a string = "welookups"

    var b, c int = 1, 2
    fmt.Println(b, c)

The output of the above example is:

1 2

Variable declaration

The first type, specifying the variable type, if not initialized, the variable defaults to zero value.

var v_name v_type
V_name = value

The zero value is the default value set by the system when the variable is not initialized.


package main
import "fmt"
func main() {

    // Declare a variable and initialize it
    var a = "welookups"

    // Zero value without initialization
    var b int

    // bool Zero value false
    var c bool

The above example execution result is:

  • Value types (including complex64/128) are 0

  • Boolean type is false

  • The string is "" (empty string)

  • The following types are nil

    var a *int
    var a []int
    var a map[string] int
    var a chan int
    var a func(string) int
    var a error // error Is the interface


package main

import "fmt"

func main() {
    var i int
    var f float64
    var b bool
    var s string
    fmt.Printf("%v %v %v %q\n", i, f, b, s)

The output is:

0 0 false ""

Second, determine the variable type by value.

var v_name = value


package main
import "fmt"
func main() {
    var d = true

The output is:


Third, omit var, note := If you don't declare a new variable on the left, a compile error occurs, in the format:

v_name := value

For example:

var intVal int 

intVal :=1 // This will generate a compilation error.

intVal,intVal1 := 1,2 //
There is no compile error when there is a new variable declared because := is a declaration statement

can var f string = "welookups" Abbreviated as f := "welookups":


package main
import "fmt"
func main() {
    f := "welookups" // var f string = "welookups"


The output is:


Multivariate Declaration

//The same type of multiple variables, non-global variables
var vname1, vname2, vname3 type
Vname1, vname2, vname3 = v1, v2, v3

var vname1, vname2, vname3 = v1, v2, v3 // Much like python, no need to display the declaration type, auto-infer

Vname1, vname2, vname3 := v1, v2, v3 // appears in:= The variable on the left should not have been declared, otherwise it will cause compilation errors

// This factoring keyword is generally used to declare global variables 
var (
    vname1 v_type1
    vname2 v_type2


package main

var x, y int
var (  // This factorization keyword is generally used to declare global variables.
    a int
    b bool

var c, d int = 1, 2
var e, f = 123, "hello"

//This format without declaration can only appear in the body of the function.
//g, h := 123, "hello"

func main(){
    g, h := 123, "hello"
    println(x, y, a, b, c, d, e, f, g, h)

The above example execution result is:

0 0 0 false 1 2 123 hello 123 hello

value type and reference type

All basic types like int, float, bool, and string are value types, and variables that use these types point directly to values that exist in memory:

When using the equal sign = to assign the value of a variable to another variable, such as: j = i, the value of i is actually copied in memory.:

You can pass &i To get the memory address of the variable i, for example:0xf840000040(Each address may be different). The value of the value type variable is stored on the stack.

The memory address will vary from machine to machine, and even the same program will have different memory addresses when executed on different machines. Because each machine may have a different memory layout and the location assignments may be different.

More complex data usually requires the use of multiple words, which are typically saved using a reference type.

A reference type variable r1 stores the memory address (number) where the value of r1 is located, or the location of the first word in the memory address.

This memory address is called a pointer, and this pointer is actually stored in another word.

The multiple words pointed to by the pointer of the same reference type can be in consecutive memory addresses (the memory layout is continuous), which is also the most efficient storage form; these words can also be used Decentralized in memory, each word indicates the memory address of the next word.

When the assignment statement r2 = r1 is used, only the reference (address) is copied.

If the value of r1 is changed, then all references to this value will point to the modified content. In this case, r2 will also be affected.

Short form, using := assignment operator

We know that the type of the variable can be omitted when the variable is initialized and the system automatically infers that the declaration of the var keyword is actually somewhat redundant, so we can abbreviate them as a := 50 or b := false.

The types of a and b (int and bool) are automatically inferred by the compiler.

This is the preferred form of using variables, but it can only be used inside functions, not for declarations and assignments of global variables. Use the operator := to efficiently create a new variable called an initialization declaration.


If in the same code block, we can not use the initialization declaration again for variables of the same name, for example: a := 20 is not allowed, the compiler will prompt the error no new variables on left side of :=, but a = 20 is ok because this gives the same variable a new value.

If you use it before defining the variable a, you will get a compile error undefined: a.

If you declare a local variable but don't use it in the same code block, you will get a compile error, such as the variable a in the following example:


package main

import "fmt"

func main() {
   var a string = "abc"
   fmt.Println("hello, world")

Trying to compile this code will get the error a declared and not used.

In addition, simply assigning a is not enough. This value must be used, so use

fmt< Span class="pun">.Println("hello, world ", a)

will remove the error.

But global variables are allowed to be declared but not used. Multiple variables of the same