Golang 变量
一个典型的程序在执行过程中会使用各种可能发生变化的数值。例如,一个程序会对用户输入的值进行一些操作。一个用户输入的值可能与另一个用户输入的值不同。因此,这使得有必要使用变量,因为另一个用户可能不会使用相同的值。当用户输入一个将在操作过程中使用的新值时,可以暂时存储在计算机的随机存取存储器中,在整个执行过程中,这部分存储器中的这些值是不同的,因此,这个术语被称为 变量。 因此,基本上,变量是一个信息的占位符,可以在运行时改变。变量允许检索和操纵存储的信息。
命名变量的规则
- 变量名称必须以字母或下划线
(_)开头。名称可以包含字母’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关键字创建的,与名称相连并提供其初始值。
语法:
var variable_name type = expression
重要的点:
- 在上述语法中,可以省略type或=表达式,但不能同时省略。
- 如果省略了=表达式,那么变量的值就由其类型的默认值决定。默认值通常为0。
- 如果删除了type,那么变量的类型由表达式中的value-initialize决定。
示例:
// 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)
}
Output:
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
- 如果表达式被删除了,那么该变量就会持有零值的类型,如数字为零,布尔运算为假,字符串为“”,接口和引用类型为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
注意:请不要混淆:=和=之间的关系,因为:=是一个声明,而=是赋值。
重要的一点是:
- 在上述表达式中,变量的类型是由表达式的类型决定的。
示例:
// 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
- 大多数局部变量都是通过使用简短的变量声明来声明和初始化的,这是因为它们简洁而灵活。
- 变量声明用于那些需要明确类型的局部变量,与初始化器表达式不同,或者用于那些稍后赋值的变量,初始化值并不重要。
- 使用简短的变量声明,你可以在一次声明中声明多个变量。
示例:
// 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
极客教程