常量
const identifier [type] = value
常量名一般全大写
显式类型定义: const B string = "abc"
隐式类型定义: const B = "abc" // 类型来自它的值
const LENGTH int = 10
const WIDTH = 5
变量声明的三种形式
-
一. 使用var声明变量
var identifier type
三者都不可缺
可以一次声明多个变量:
var identifier1, identifier2 type
以上没有赋值的声明默认赋予该类型的零值
-
二. 当声明和赋值一起时,可以省略type
var message = "hello world"
此处message类型为string,来自其值 -
三. 声明和赋值一起的另一种形式
message := "hello world"
不能重复声明,也不能不声明
变量赋值
message = "hello world"
message必须是已声明变量
网友评论