典型的程序使用在执行过程中可能会更改的各种值。例如,该程序对用户输入的值执行一些操作。一个用户输入的值可能与另一用户输入的值不同。因此,这有必要使用变量,因为另一个用户可能不会使用相同的值。当用户输入将在操作过程中使用的新值时,可以将其临时存储在计算机的随机访问内存中,并且这些内存值在整个执行过程中都将有所不同,因此出现了另一个术语作为变量。因此,基本上,变量是可以在运行时更改的信息的占位符。并且变量允许检索和处理存储的信息。
命名变量规则:
变量名称必须以字母或下划线()开头。并且名称中可能包含字母“ 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 keyowrd 创建变量,并与名称关联并提供其初始值。
句法:
var variable_name type = 表达式
重要事项:
- 在以上语法中,类型或=表达式可以删除,但不能同时删除变量声明中的两个。
如果删除了类型,则变量的类型由表达式中的值初始化确定。
例:
```
// 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 = "GeeksforGeeks" 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 : GeeksforGeeks 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:=表达式
注意:请不要在:=和=之间混淆,因为:=是声明,而=是赋值。
重要事项:
在上面的表达式中,变量的类型由表达式的类型确定。
例:
// Go program to illustrate // concept of variable package main import "fmt" func main() { // Using short variable declaration myvar1 := 39 myvar2 := "GeeksforGeeks" 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 : GeeksforGeeks 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 ```
© 著作权归作者所有
发表评论