这是我们Golang基础教程 的第三节,介绍Golang中的变量相关内容。
什么是变量
变量用来存储某种类型的值,Golang中有若干种不同的声明变量的语句。
声明单个变量
var name type
用来声明单个变量。
Copy package main
import "fmt"
func main() {
var age int // 变量声明
fmt.Printf("my age is %d\n", age)
}
点击这里 使用Go Playground在线运行。
前面的语句var age int
声明了一个int
类型的变量age
,但是并没有给该变量赋值。在Golang中,如果一个变量声明时未手动赋值,Golang会自动将该变量初始化为对应类型的零值 (注意每种类型都有其独特的零值,比如int
类型的零值为0)。这里由于age
声明为了int
类型,因此Golang自动将该变量初始化为0。运行该程序会输出如下结果:
我们可以给某种类型的变量赋予该类型的任何值,比如这里的age
变量,我们可以赋予任何大小的整数值:
Copy package main
import "fmt"
func main() {
var age int // 变量声明
fmt.Printf("my age is %d\n", age)
age = 29 // 给age变量赋值
fmt.Printf("my age is %d\n", age)
age = 54 // 给age变量赋另一个值
fmt.Printf("my age is %d\n", age)
}
点击这里 使用Go Playground在线运行。
该程序输出如下:
Copy my age is 0
my age is 29
my age is 54
变量声明的同时赋初值
变量声明的同时可以赋初值,语法如下:var name type = initialvalue
。
Copy package main
import "fmt"
func main() {
var age int = 29 // 变量声明并赋初值
fmt.Printf("my age is %d\n", age)
}
点击这里 使用Go Playground在线运行。
这段程序中变量age
被声明为int
类型,同时被赋予了初值29
,输出如下:
类型推断
如果一个变量声明的同时并进行了赋值,那么Golang可以自动根据赋予的初值来推断出变量的类型,因此这种情况下声明变量时可以将声明语句var type name = initalvalue
中的type
字段省掉,变为如下形式:var name = initialvalue
。
下面的程序中age
变量声明时赋予了初值29
,因此Golang可以自动推断出age
变量的类型为int
。
Copy package main
import "fmt"
func main() {
var age = 29 // 根据初始值自动推断age类型
fmt.Printf("my age is %d\n", age)
}
点击这里 使用Go Playground在线运行。
同时声明相同类型的多个变量
在Golang中,单个语句可以同时声明某种相同类型的多个变量,语法如下:var name1, name2 type = initialvalue1, initialvalue2
。
Copy package main
import "fmt"
func main() {
var width, height int = 100, 50 // 同时声明两个变量
fmt.Println("width is ", width, "height is ", height)
}
点击这里 使用Go Playground在线运行。
这段程序中由于name1
和name2
两个变量声明时并赋予了初值,因此Golang可以推断出变量类型,因此可以省去type
字段:var width, height = 100, 50
。
如果这两个变量声明时不赋初值,那么就不能省去type
字段,这时Golang会自动初始化为零值
。
Copy package main
import "fmt"
func main() {
var width, height int
fmt.Println("width is", width, "height is", height)
width = 100
height = 50
fmt.Println("new width is", width, "new height is ", height)
}
点击这里 使用Go Playground在线运行。
该程序输出如下:
Copy width is 0 height is 0
new width is 100 new height is 50
同时声明不同类型的多个变量
当然了,Golang也可以同时声明不同类型的多个变量,语法如下:
Copy var (
name1 = initialvalue1
name2 = inititalvalue2
name3 type3
)
举个例子:
Copy package main
import "fmt"
func main() {
var (
name = "naveen"
age = 29
height int
)
fmt.Println("my name is", name, ", age is", age, "and height is", height)
}
Go Playground在线运行
这里同时声明了3个变量,string
类型的name
,int
类型的age
和height
,该程序输出如下:
Copy my name is naveen , age is 29 and height is 0
如果我们想要声明的不同类型的多个变量均提供手动初始值,那么也可以采用下面的形式:
Copy package main
import "fmt"
func main() {
var name, age = "naveen", 29
fmt.Println("my name is", name, ", age is", age)
}
Go Playground在线运行
变量声明简写形式
Golang提供了一种非常简洁的声明变量的缩写形式,语法如下:name := value
,注意这种方法要求手动赋予变量初值。
Copy package main
import "fmt"
func main() {
name, age := "naveen", 29 // 变量声明简写
fmt.Println("my name is", name, "age is", age)
}
Go Playground在线运行
执行结果如下:
Copy my name is naveen age is 29
切记,变量声明简写形式要求所有声明的变量都在:=
右侧手动赋予初值。下面的程序运行会报错:
Copy assignment mismatch: 2 variable but 1 values
就是因为变量age
未在:=
右侧手动赋予初值:
Copy package main
import "fmt"
func main() {
name, age := "naveen" //error
fmt.Println("my name is", name, "age is", age)
}
Go Playground在线运行
仅新声明的变量支持简写声明赋值
仅当:=
左侧声明的变量至少一个是新 声明变量时才可使用简写形式。
Copy package main
import "fmt"
func main() {
a, b := 20, 30 // declare variables a and b
fmt.Println("a is", a, "b is", b)
b, c := 40, 50 // b is already declared but c is new
fmt.Println("b is", b, "c is", c)
b, c = 80, 90 // assign new values to already declared variables b and c
fmt.Println("changed b is", b, "c is", c)
}
Go Playground在线运行
这段程序中语句b, c := 40, 50
之所以合法是因为虽然变量b
前面已声明过,但是变量c
是新声明的变量,执行结果如下:
Copy a is 20 b is 30
b is 40 c is 50
changed b is 80 c is 90
但是如果是下面的程序,则执行时会报错:
Copy no new variables on left side of :=
是因为变量a
,b
前面均已声明过。
Copy package main
import "fmt"
func main() {
a, b := 20, 30 //a and b declared
fmt.Println("a is", a, "b is", b)
a, b := 40, 50 //error, no new variables
}
Go Playground在线运行
Golang中变量赋值不仅可以赋予字面量值,还可以赋予运行时计算结果值。
Copy package main
import (
"fmt"
"math"
)
func main() {
a, b := 145.8, 543.8
c := math.Min(a, b)
fmt.Println("minimum value is ", c)
}
Go Playground在线运行
这段程序中变量c
被赋予的值即为程序运行时计算结果值。注意,由于Golang是强类型编程语言,因此给变量赋值时只能赋予类型匹配的值,否则会报错。