Variables can be defined as containers for storing data values. The variable value can be changed after the initial value is set. The var statement can be used to declare a variable or list of variables, with the type at the end of it.
变量可以定义为存储数据值的容器。初始值设置完成后,可以修改变量值。var语句可用于声明一个变量或变量列表,并在其末尾加上类型。
This code sample will declare four integer variables and one Boolean variable:
下面的代码示例将声明四个整型变量和一个布尔变量:
go
var a, b, c int
var d bool
The var statement can include initializers; the number of initializers must be the same as the number of variable names. If the initializer is not present, the default value will be assigned to the variable.
var语句可以包含初始化式;初始化式的数量必须与变量名的数量相同。如果初始化项不存在,则将默认值赋给该变量。
Here is the same code block from the previous example with initializers:
下面是与前面带有初始化式的示例相同的代码块:
go
var a, b, c int = 1, 2, 3
var d bool = true
Too see differences between initialized and uninitialized variables, we can execute this code in Go Playground:
为了了解初始化和未初始化变量之间的区别,我们可以在Go Playground中执行以下代码:
go
var a int
var b bool
var c = 1
var d = true
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
As we can see, default value 0 and false will be assigned to uninitialized variables a and b, while specific values 1 and true will be assigned to initialized variables c and d.
正如我们所看到的,默认值0和false将被分配给未初始化的变量a和b,而特定值1和true将被分配给初始化的变量c和d。
If initilizers are present, we can omit type and the variable will inherit type from the initializer. So, the previous examples can be shortened:
如果存在初始化项,可以省略type,变量将从初始化项继承type。所以,前面的例子可以缩写为:
go
var a, b, c, d = 1, 2, 3, true
Based on where they are declared, we have the following variable:
- Local variables: Declared and used inside functions. They cannot be accessed from outside of the functions in which they are declared.
- Global variables: Declared in a package or outside of functions. They can be accessed globally from other packages(must be exported).
根据它们的声明位置,我们有以下变量:
- 局部变量:在函数内部声明和使用。不能从声明它们的函数外部访问它们。
- 全局变量:在包内或函数外声明。可以从其他包全局访问它们(必须导出)。
In this example, a is global, while b is a local variable:
在这个案例中,a是全局变量,而b是局部变量:
go
var a int
func main(){
var b int
}
Variable can be declared and initialized with a short assignment. The following two statements are equal:
变量可以更简短的声明,下面的两个变量赋值是等价的:
go
var i int = 1
i := 1
In the Go programming language, all statements outside the functions must begin with the keyword var, func or const. So, shourt assignment statements can be only used for local variables.
在Go编程语言中,函数之外的所有语句必须以关键字var、func或const开头。因此,短赋值语句只能用于局部变量。
类型转换
Many programming languages support a concept called implicit conversion. If we have a numberic variable that is floating point type and try to assign value 7(integer value), the programming language which supports implicit conversion will convert the value to 7.0 and assign it to a variable. Go does not support implicit conversion, so the value must be explicitly converted to a specific type.
许多编程语言都支持一种称为隐式转换的概念。如果我们有一个浮点类型的数字变量,并尝试赋值为7(整数值),支持隐式转换的编程语言会将值转换为7.0并将其赋值给变量。Go不支持隐式转换,因此必须显式地将值转换为特定类型。
If we try to execute the following statements, the compiler will report an error on the second one:
如果我们尝试执行以下语句,编译器将在第二个语句中报告错误:
go
var a int32 = 7
var b float32 = a // fails
Expression T(v) will convert value v to type T. Conversion from our example (integer to floating point) can be executed with this statement:
表达式T(v)将值v转换为类型T。从我们的例子(整型到浮点型)的转换可以用下面的语句执行:
go
var b float32 = float32(b)
常量
Constants are values cannot be changed once defined. Constants are declared like variables but with the const keyword instead of var and cannot be declared with a short statement. We can create constants from any basic data type, like integer constants, or floating-point constants. String constants are called string literals. Usually, constant names are written in capital letters with the underscore character used to separate words.
常量是一旦定义就不能更改的值。常量可以像变量一样声明,但使用const关键字而不是var,并且不能用短语句声明。我们可以从任何基本数据类型创建常量,如整数常量或浮点常量。字符串常量称为字符串字面值。通常,常量名称用大写字母写成,并使用下划线分隔单词。
These statements will create string literal and floating-point constants:
这些语句将创建字符串常量和浮点常量:
go
const HELLO_WORLD = "Hello world!"
const GOLDEN_RATIO = 1.618
Very often, we want to assign successive integer values to constants, like this:
通常,我们希望将连续整数值赋值给常量,如下所示:
go
const (
ZERO = 0
ONE = 1
TWO = 2
)
This can be done more elegantly with iota keyword. It represents successive integer constants(0,1,2,...), so the previous code segment can be written in the following way:
使用iota关键字可以更优雅地做到这一点。它表示连续的整数常量(0,1,2,...),因此前面的代码段可以用以下方式编写:
go
const (
ZERO = iota
ONE
TWO
)
Value 0 is always the first one in sequence, but we can use arithmetic operators to start from another value, like in this example:
值0总是序列中的第一个,但我们可以使用算术运算符从另一个值开始,如下例所示:
go
const (
ONE = iota + 1
TWO
THREE
)
The iotal will be reset to 0 whenever the keyword const appears in the source code. In the following example, value 0 will be assigned to constants ZERO and TEST:
每当关键字const出现在源代码中时,总数将重置为0。在下面的例子中,值0将被分配给常量ZERO和TEST:
go
const (
ZERO = iota
ONE
TWO
)
const TEST = iota