Go programming examples [variables]

base instance

In Go, variables are explicitly declared and can be used by the compiler to check the type correctness of function calls.

// variables.go
package main

import "fmt"

func main() {
    
    

	// `var` 声明 1 个或者多个变量。
	var a = "initial"
	fmt.Println(a)

	// 你可以一次性声明多个变量。
	var b, c int = 1, 2
	fmt.Println(b, c)

	// Go 将自动推断已经初始化的变量类型。
	var d = true
	fmt.Println(d)

	// 声明后却没有给出对应的初始值时,变量将会初始化为
	// _零值_ 。例如,一个 `int` 的零值是 `0`。
	var e int
	fmt.Println(e)

	// `:=` 语法是声明并初始化变量的简写,例如
	// 这个例子中的 `var f string = "short"`。
	f := "short"
	fmt.Println(f)
}
[root@bogon test]# go run variables.go
initial
1 2
true
0
short
[root@bogon test]# 

Brief description of variables (Variables)

What are variables?

A variable specifies the name of a memory location (Memory Location) that stores a value of a particular type.
In Go, there are various syntaxes for declaring variables.

declare a single variable

var name typeis the syntax for declaring a single variable.

package main

import "fmt"

func main() {
    
    
    var age int // 变量声明
    fmt.Println("my age is", age)
}

Run the program online

The statement var age int declares a variable of type int named age. We haven't assigned a value to this variable yet. If the variable has not been assigned a value, Go will automatically initialize it, assigning the zero value (Zero Value) of the variable type.

In this example age is assigned a value of 0.
If you run the program, you will see the following output:my age is 0

Variables can be assigned any value of this type.
The age in the previous program can be assigned any integer value (Integer Value).

package main

import "fmt"

func main() {
    
    
    var age int // 变量声明
    fmt.Println("my age is", age)
    age = 29 // 赋值
    fmt.Println("my age is", age)
    age = 54 // 赋值
    fmt.Println("my new age is", age)
}

Run the program online

The above program will produce the following output:

my age is  0
my age is 29
my new age is 54

Declare variables and initialize

Initial values ​​can be given while declaring variables.

var name type = initialvaluesyntax for declaring variables and initializing them.

package main

import "fmt"

func main() {
    
    
    var age int = 29 // 声明变量并初始化

    fmt.Println("my age is", age)
}

Run the program online

In the above program, age is a variable of type int with an initial value of 29.
If you run the above program, you can see the output below, confirming that age has been initialized to 29.

my age is 29

Type Inference¶

If a variable has an initial value, then Go can automatically infer the type of the variable with the initial value.
Therefore, type can be omitted from the variable declaration if the variable has an initial value.

If the syntax of the variable declaration is var name = initialvalue, Go can automatically infer the type of the variable based on the initial value.

In the following example, we omit the int type of the variable age, Go still deduces that it is of type int.

package main

import "fmt"

func main() {
    
    
    var age = 29 // 可以推断类型

    fmt.Println("my age is", age)
}

declare multiple variables

Go has the ability to declare multiple variables with a single statement.

The syntax for declaring multiple variables is var name1, name2 type = initialvalue1, initialvalue2.

package main

import "fmt"

func main() {
    
    
    var width, height int = 100, 50 // 声明多个变量

    fmt.Println("width is", width, "height is", heigh)
}

Run the program online

The above program will print width is 100 height is 50 to standard output.

You might have thought that if width and height were omitted from initialization, their initial values ​​would be zero.

package main

import "fmt"

func main() {
    
    
    var width, height int
    fmt.Println("width is", width, "height is", height)
    width = 100
    height = 50
    fmt.Println("new width is", width, "new height is ", height)
}
width is 0 height is 0
new width is 100 new height is  50

In some cases, we may want to declare variables of different types in one statement.
Its syntax is as follows:

var (
    name1 = initialvalue1,
    name2 = initialvalue2
)

Using the above syntax, the following program declares variables of different types.

package main

import "fmt"

func main() {
    
    
    var (
        name   = "naveen"
        age    = 29
        height int
    )
    fmt.Println("my name is", name, ", age is", age, "and height is", height)
}

Here we declare name of type string, age and height of type int.
Running the above program produces the output my name is naveen , age is 29 and height is 0.

brief statement

Go also supports a concise form of declaring variables
called a Short Hand Declaration, which uses :=the operator .

The short syntax for declaring variables is name := initialvalue.

package main

import "fmt"

func main() {
    
    
    name, age := "naveen", 29 // 简短声明

    fmt.Println("my name is", name, "age is", age)
}

Running the above program, you can see the output as my name is naveen age is 29.

Short declarations require that all variables to the left of :=the operator have initial values.

The following program will throw an error cannot assign 1 values ​​to 2 variables, because age has not been assigned.

package main

import "fmt"

func main() {
    
    
    name, age := "naveen" //error

    fmt.Println("my name is", name, "age is", age)
}

The syntax for short declarations requires at least one variable to the left of :=the operator to be undeclared.

Consider the following program:

package main

import "fmt"

func main() {
    
    
    a, b := 20, 30 // 声明变量a和b
    fmt.Println("a is", a, "b is", b)
    
    b, c := 40, 50 // b已经声明,但c尚未声明
    fmt.Println("b is", b, "c is", c)
    
    b, c = 80, 90 // 给已经声明的变量b和c赋新值
    fmt.Println("changed b is", b, "c is", c)
}

Since b has been declared but c has not, the run succeeds and outputs:

a is 20 b is 30
b is 40 c is 50
changed b is 80 c is 90

But if we run the following program:

package main

import "fmt"

func main() {
    
    
    a, b := 20, 30 // 声明a和b
    fmt.Println("a is", a, "b is", b)
    a, b := 40, 50 // 错误,没有尚未声明的变量
}

It will throw after the above operation no new variables on left side of := 的错误, because the variables of a and b have been declared, :=and there are no undeclared variables on the left.

Variables can also be assigned values ​​at runtime.
Consider the following program:

package main

import (
    "fmt"
    "math"
)

func main() {
    
    
    a, b := 145.8, 543.8
    c := math.Min(a, b)
    fmt.Println("minimum value is ", c)
}

In the above program, the value of c is calculated during operation, which is the minimum value of a and b.
The above program will print:

minimum value is  145.8

Since Go is a strongly typed language, it does not allow variables of one type to be assigned values ​​of other types.

The following program throws an error cannot use "naveen" (type string) as type int in assignmentbecause age is declared to be of type int and we try to assign a value of type string to it.

package main

func main() {
    
    
    age := 29      // age是int类型
    age = "naveen" // 错误,尝试赋值一个字符串给int类型变量
}

Guess you like

Origin blog.csdn.net/weiguang102/article/details/129734631