Home  >  Article  >  Backend Development  >  What are variables in Go language

What are variables in Go language

青灯夜游
青灯夜游Original
2022-12-02 19:53:365674browse

In the Go language, a variable is an amount that can change (modify) its content during the running of the program; a variable is the basic unit of the program and represents a data storage space in the memory. It can be found by the variable name. variable. The function of variables is to store user data. It is an abstract concept in computer language that can store calculation results or represent values.

What are variables in Go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

1. Overview

Variable (Variable) is the amount that the content can change (modify) during the running of the program; Variables are the program's The basic unit is a representation of a data storage space in memory. The variable value can be found through the variable name.

The function of variables is to store user data. It is an abstract concept in computer language that can store calculation results or represent values.

Variable is the process of locating the value through the identifier of the variable. Memory simulation demonstration of variables:

What are variables in Go language

2. Definition: declaration and initialization

Use var to define variables Keyword, its type needs to be specified, and can be initialized at the same time. If no initialization value is specified, the default value of the type is used. The syntax is as follows:

// 声明变量,未初始化(默认值初始化)
var name string
// 声明变量,特定值初始化
var user string = "Hank"

Variables in the Go language must be declared. Can be used, undefined variables cannot be used directly.

3. Type default value (zero value)

The type default value of Go language is as follows: Integer and float The default value for point variables is 0. The default value of a string variable is the empty string. Boolean variables default to bool. The default for slices, functions, and pointer variables is nil.

4. Strong typing

Go language is a strongly typed language, variables must have types, and variables can only store specific types of data. [Related recommendations: Go video tutorial]

5. Type derivation

When defining a variable, if an initial value, you can omit the definition of the type, and the Go language can deduce the type from the data itself. The syntax is:

// 声明变量,特定值初始化,类型推导出user为字符串string型
var user = "Hank"

6. Short declaration (definition)

To simplify defining variables, use operators: = With type derivation, you can quickly complete the definition of variables. The syntax is:

user := "hank"

7. Batch definition

You can use var or := to define multiple variables at once. The syntax is:

var

var (
  v1 = 42
  v2 = "hank"
  )
也可以
var v1, v2 = 42, "hank"

:=

v1, v2 := 42, "hank"

It is recommended to use var() declaration block syntax because code maintenance is easy.

8. Batch assignment

Assign multiple variables at one time. (Similar to short definition. v1, v2, v3 := 42, "hank", false) After the variables are defined, assign values ​​to them in batches. There is no definition function! The syntax is:

var (
  v1 int
  v2 int
)
// 批量赋值
v1, v2 = 42, 1024

This syntax is usually used with the batch return of the function to receive multiple return values ​​at one time. The syntax is:

func main() {
  var (
    v1 int
    v2 int
  )
  // 调用函数,接收其返回值
  v1, v2 = getData()
}
// 返回两个值的函数
func getData() (int, int) {
  // 返回了两个值(假设42为最小值,1024为最大值)
  return 42, 1024
}

It is very convenient to exchange the values ​​of two variables:

var (
  v1 = 42
  v2 = 1024
)
// 交换
v1, v2 = v2, v1

After execution, v1==1024, v2==42

9. Anonymous variables

Variables with the identifier _ (underscore) are anonymous variables reserved by the system. After assignment, they will be released immediately and are called anonymous variables. Its function is a variable placeholder, assigning a value structure to its variable. Usually used during batch assignment. For example, if the function returns multiple values ​​and we only need some of them, then use _ to occupy the space if not necessary. Demonstration:

func main() {
  var (
    v int
  )
  // 调用函数,仅仅需要第二个返回值,第一,三使用匿名变量占位
  _, v, _ = getData()
}
// 返回两个值的函数
func getData() (int, int, int) {
  // 返回3个值
  return 42, 1024, 2012
}

10. Variable scope

Variables in Go language are block scoped. Blocks refer to code segments defined using {}, including functions, if/switch/for statements, or independent {} are all blocks. Variables defined within a block are only available within this block. Definition refers to variables defined by var or :=. The scope of the Go language is cascading, which means that the inner block can directly access the variables of the outer block, provided that the inner block does not define an outer variable with the same name. The demonstration is as follows:

// 全局(函数外)定义3个变量
var (
  v1 = 1
  v2 = 2
  v3 = 3
)
func main() {

  // main{}函数块,定义2个变量
  var (
    v2 = 22
    v3 = 33
  )
  fmt.Println(v1, v2, v3)
  // 结果 1 22 33

  {
    // main()函数内部{}块,定义1个变量
    var (
      v3 = 333
    )
    fmt.Println(v1, v2, v3)
    // 结果 1 22 333
  }
}

上面代码中: 在main()中,v2,v3被重新定义,则在main()中,v1是外部,而v2, v3是main函数局部的。 在main()内部的{}中,v3被重新定义,则在main()内部的{}中,v1是外部,而v2是main()定义的, v3是main()内部{}定义的的。

变量可以沿内部作用域向外部作用域查找变量的过程。

带有{}的语句,其中的变量也仅仅在内部有效,例如for,if,switch等,演示:

for i := 0; i < 10; i++ {
  fmt.Println(i)
}
fmt.Println(i)
// 会提示变量i未定义, undefined: i

注意i,是通过短声明在for内部定义的变量,仅仅在for内部有意义。

互不嵌套的{}见作用域不可见,不可互相访问。

func main() {
  {
    v := 42
  }
  {
    fmt.Println(v)
    // 会提示变量v未定义, undefined: v
  }
}

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of What are variables in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn