变量是用于保存值的存储位置或占位符。它允许我们操纵和检索存储的信息。 Golan中有两种声明变量的方式,如下所示:
- 使用 var 关键字
- 使用短声明运算符( := )
var 关键字和短声明运算符的区别
var keyword | short declaration operator |
---|---|
var is a lexical keyword present in Golang. | := is known as the short declaration operator. |
It is used to declare and initialize the variables inside and outside the functions. | It is used to declare and initialize the variables only inside the functions. |
Using this, variables have generally package level or global level scope. It can also have local scope. | Here, variables has only local scope as they are only declared inside functions. |
Declaration and initialization of the variables can be done separately. | Declaration and initialization of the variables must be done at the same time. |
It is optional to put type along with the variable declaration. | There is no need to put type. If you, it will give error. |
示例 1:在此程序中,您可以看到myvariable1是使用var关键字声明的,并且它具有局部作用域。 myvariable2也使用 var 关键字和 int 类型声明,但没有进行初始化。所以它将采用 int 类型的默认值,即零(您可以在输出中看到)。 myvariable3是使用短变量声明运算符声明和初始化的,它具有局部作用域。
// Go program to show the use of var lexical
// keyword and short declaration operator
package main
import (
"fmt"
)
func main() {
// using var keyword to declare
// and initialize the variable
var myvariable1 = 100
fmt.Println(myvariable1)
// using var keyword to declare
// the variable along with type
var myvariable2 int
fmt.Println(myvariable2)
// using short variable declaration
myvariable3 := 200
fmt.Println(myvariable3)
}
输出:
100
0
200
示例 2:在此程序中,您可以看到myvariable1是使用var关键字声明的,它具有全局作用域。 myvariable2也使用 var 关键字和 int 类型声明,但没有进行初始化。所以它将采用 int 类型的默认值,即零(您可以在输出中看到)。 myvariable3是使用短变量声明运算符声明和初始化的,它具有局部作用域。
// Go program to show the use of var lexical
// keyword and short declaration operator
package main
import (
"fmt"
)
// using var keyword to declare
// and initialize the variable
// it is package or you can say
// global level scope
var myvariable1 = 100
func main() {
// accessing myvariable1 inside the function
fmt.Println(myvariable1)
// using var keyword to declare
// the variable along with type
var myvariable2 int
fmt.Println(myvariable2)
// using short variable declaration
myvariable3 := 200
fmt.Println(myvariable3)
}
输出:
100
0
200
示例 3:在此程序中,您可以看到myvariable1是使用var关键字声明的,它具有全局作用域。 myvariable2也使用 var 关键字和 int 类型声明,但没有进行初始化。所以它将采用 int 类型的默认值,即零(您可以在输出中看到)。 myvariable3是在函数外使用短变量声明运算符声明和初始化的,这是不允许的,因此会产生错误。
// Go program to show the use of var lexical
// keyword and short declaration operator
package main
import (
"fmt"
)
// using var keyword to declare
// and initialize the variable
// it is package or you can say
// global level scope
var myvariable1 = 100
// using short variable declaration
// it will give an error as it is not
// allowed outside the function
myvariable3 := 200
func main() {
// accessing myvariable1 inside the function
fmt.Println(myvariable1)
// using var keyword to declare
// the variable along with type
var myvariable2 int
fmt.Println(myvariable2)
fmt.Println(myvariable3)
}
错误:
./prog.go:18:1: syntax error: non-declaration statement outside function body