典型的程序使用在执行过程中可能会更改的各种值。例如, 该程序对用户输入的值执行一些操作。一个用户输入的值可能与另一用户输入的值不同。因此, 这有必要使用变量, 因为另一个用户可能不会使用相同的值。当用户输入将在操作过程中使用的新值时, 可以将其临时存储在计算机的随机存取存储器中, 并且这些值在存储器的此部分中在整个执行过程中会有所不同, 因此出现了另一个术语如变量。因此, 基本上, 变量是可以在运行时更改的信息的占位符。并且变量允许检索和处理存储的信息。
命名变量规则:
- 变量名称必须以字母或下划线(_)开头。并且名称中可能包含字母" a-z"或" A-Z"或数字0-9, 以及字符" _"。
Geeks, geeks, _geeks23 // valid variable 123Geeks, 23geeks // invalid variable
- 变量名称不应以数字开头。
234geeks // illegal variable
- 变量名称区分大小写。
geeks and Geeks are two different variables
- 关键字不允许用作变量名。
- 变量名称的长度没有限制, 但是建议仅使用4到15个字母的最佳长度。
声明一个变量
在Go语言中, 变量是通过两种不同的方式创建的:
使用var关键字:
在Go语言中, 变量是使用
变种
与名称关联并提供其初始值的特定类型的关键字。
语法如下:
var variable_name type = expression
重要事项:
在以上语法中,
类型
or
=
表达式可以删除变量中的声明, 但不能两者都删除。
如果删除了类型, 则变量的类型由表达式中的值初始化确定。
例子:
// Go program to illustrate
// concept of variable
package main
import "fmt"
func main() {
// Variable declared and
// initialized without the
// explicit type
var myvariable1 = 20
var myvariable2 = "lsbin"
var myvariable3 = 34.80
// Display the value and the
// type of the variables
fmt.Printf( "The value of myvariable1 is : %d\n" , myvariable1)
fmt.Printf( "The type of myvariable1 is : %T\n" , myvariable1)
fmt.Printf( "The value of myvariable2 is : %s\n" , myvariable2)
fmt.Printf( "The type of myvariable2 is : %T\n" , myvariable2)
fmt.Printf( "The value of myvariable3 is : %f\n" , myvariable3)
fmt.Printf( "The type of myvariable3 is : %T\n" , myvariable3)
}
输出如下:
The value of myvariable1 is : 20
The type of myvariable1 is : int
The value of myvariable2 is : lsbin
The type of myvariable2 is : string
The value of myvariable3 is : 34.800000
The type of myvariable3 is : float64
如果删除了表达式, 则该变量将为类型保留零值, 例如数字为零, 布尔值为false,
""
表示字符串, nil表示接口和引用类型。所以, 有
Go语言中没有这样的未初始化变量的概念。
例子:
// Go program to illustrate
// concept of variable
package main
import "fmt"
func main() {
// Variable declared and
// initialized without expression
var myvariable1 int
var myvariable2 string
var myvariable3 float64
// Display the zero-value of the variables
fmt.Printf( "The value of myvariable1 is : %d\n" , myvariable1)
fmt.Printf( "The value of myvariable2 is : %s\n" , myvariable2)
fmt.Printf( "The value of myvariable3 is : %f" , myvariable3)
}
输出如下:
The value of myvariable1 is : 0
The value of myvariable2 is :
The value of myvariable3 is : 0.000000
如果使用类型, 则可以在单个声明中声明相同类型的多个变量。
例子:
// Go program to illustrate
// concept of variable
package main
import "fmt"
func main() {
// Multiple variables of the same type
// are declared and initialized
// in the single line
var myvariable1, myvariable2, myvariable3 int = 2, 454, 67
// Display the values of the variables
fmt.Printf( "The value of myvariable1 is : %d\n" , myvariable1)
fmt.Printf( "The value of myvariable2 is : %d\n" , myvariable2)
fmt.Printf( "The value of myvariable3 is : %d" , myvariable3)
}
输出如下:
The value of myvariable1 is : 2
The value of myvariable2 is : 454
The value of myvariable3 is : 67
如果删除类型, 则可以在单个声明中声明不同类型的多个变量。变量的类型由初始化值确定。
例子:
// Go program to illustrate
// concept of variable
package main
import "fmt"
func main() {
// Multiple variables of different types
// are declared and initialized in the single line
var myvariable1, myvariable2, myvariable3 = 2, "GFG" , 67.56
// Display the value and
// type of the variables
fmt.Printf( "The value of myvariable1 is : %d\n" , myvariable1)
fmt.Printf( "The type of myvariable1 is : %T\n" , myvariable1)
fmt.Printf( "\nThe value of myvariable2 is : %s\n" , myvariable2)
fmt.Printf( "The type of myvariable2 is : %T\n" , myvariable2)
fmt.Printf( "\nThe value of myvariable3 is : %f\n" , myvariable3)
fmt.Printf( "The type of myvariable3 is : %T\n" , myvariable3)
}
输出如下:
The value of myvariable1 is : 2
The type of myvariable1 is : int
The value of myvariable2 is : GFG
The type of myvariable2 is : string
The value of myvariable3 is : 67.560000
The type of myvariable3 is : float64
返回多个值的调用函数允许你初始化一组变量。
例子:
// Here, os.Open function return a
// file in i variable and an error
// in j variable
var i, j = os.Open(name)
使用简短的变量声明:
通过使用短变量声明来声明在函数中声明和初始化的局部变量。
语法如下:
variable_name:= expression
注意:请不要在两者之间混淆:=和=as:=是一个声明, =是分配。
重要事项:
在上面的表达式中, 变量的类型由表达式的类型确定。
例子:
// Go program to illustrate
// concept of variable
package main
import "fmt"
func main() {
// Using short variable declaration
myvar1 := 39
myvar2 := "lsbin"
myvar3 := 34.67
// Display the value and type of the variables
fmt.Printf( "The value of myvar1 is : %d\n" , myvar1)
fmt.Printf( "The type of myvar1 is : %T\n" , myvar1)
fmt.Printf( "\nThe value of myvar2 is : %s\n" , myvar2)
fmt.Printf( "The type of myvar2 is : %T\n" , myvar2)
fmt.Printf( "\nThe value of myvar3 is : %f\n" , myvar3)
fmt.Printf( "The type of myvar3 is : %T\n" , myvar3)
}
输出如下:
The value of myvar1 is : 39
The type of myvar1 is : int
The value of myvar2 is : lsbin
The type of myvar2 is : string
The value of myvar3 is : 34.670000
The type of myvar3 is : float64
由于它们的简洁性和灵活性, 大多数局部变量都是使用短变量声明来声明和初始化的。
变量的var声明用于那些需要与初始值设定项表达式不同的显式类型的局部变量, 或用于其值稍后分配且初始值不重要的变量。
使用短变量声明, 可以在单个声明中声明多个变量。
例子:
// Go program to illustrate
// concept of variable
package main
import "fmt"
func main() {
// Using short variable declaration
// Multiple variables of same types
// are declared and initialized in
// the single line
myvar1, myvar2, myvar3 := 800, 34, 56
// Display the value and
// type of the variables
fmt.Printf( "The value of myvar1 is : %d\n" , myvar1)
fmt.Printf( "The type of myvar1 is : %T\n" , myvar1)
fmt.Printf( "\nThe value of myvar2 is : %d\n" , myvar2)
fmt.Printf( "The type of myvar2 is : %T\n" , myvar2)
fmt.Printf( "\nThe value of myvar3 is : %d\n" , myvar3)
fmt.Printf( "The type of myvar3 is : %T\n" , myvar3)
}
输出如下:
The value of myvar1 is : 800
The type of myvar1 is : int
The value of myvar2 is : 34
The type of myvar2 is : int
The value of myvar3 is : 56
The type of myvar3 is : int
在简短的变量声明中, 允许返回多个值的调用函数初始化一组变量。
例子:
// Here, os.Open function return
// a file in i variable and an
// error in j variable
i, j := os.Open(name)
简短的变量声明仅当对于已在同一词法块中声明的变量时才像赋值一样。在外部块中声明的变量将被忽略。如下面的示例所示, 这两个变量中至少有一个是新变量。
例子:
// Go program to illustrate
// concept of variable
package main
import "fmt"
func main() {
// Using short variable declaration
// Here, short variable declaration acts
// as an assignment for myvar2 variable
// because same variable present in the same block
// so the value of myvar2 is changed from 45 to 100
myvar1, myvar2 := 39, 45
myvar3, myvar2 := 45, 100
// If you try to run the commented lines, // then compiler will gives error because
// these variables are already defined
// myvar1, myvar2 := 43, 47
// myvar2:= 200
// Display the values of the variables
fmt.Printf( "The value of myvar1 and myvar2 is : %d %d\n" , myvar1, myvar2)
fmt.Printf( "The value of myvar3 and myvar2 is : %d %d\n" , myvar3, myvar2)
}
输出如下:
The value of myvar1 and myvar2 is : 39 100
The value of myvar3 and myvar2 is : 45 100
使用短变量声明, 你可以在单个声明中声明多个不同类型的变量。这些变量的类型由表达式确定。
例子:
// Go program to illustrate
// concept of variable
package main
import "fmt"
func main() {
// Using short variable declaration
// Multiple variables of different types
// are declared and initialized in the single line
myvar1, myvar2, myvar3 := 800, "Geeks" , 47.56
// Display the value and type of the variables
fmt.Printf( "The value of myvar1 is : %d\n" , myvar1)
fmt.Printf( "The type of myvar1 is : %T\n" , myvar1)
fmt.Printf( "\nThe value of myvar2 is : %s\n" , myvar2)
fmt.Printf( "The type of myvar2 is : %T\n" , myvar2)
fmt.Printf( "\nThe value of myvar3 is : %f\n" , myvar3)
fmt.Printf( "The type of myvar3 is : %T\n" , myvar3)
}
输出如下:
The value of myvar1 is : 800
The type of myvar1 is : int
The value of myvar2 is : Geeks
The type of myvar2 is : string
The value of myvar3 is : 47.560000
The type of myvar3 is : float64