What is a constant?
Constants are used in Go language to represent fixed values, for example:
1 2 3 |
|
The keyword const is used to declare a constant. Let’s take a look at how to declare constants in Go language:
1 2 3 4 5 6 7 8 9 10 |
|
Execution[1]
The above code declares the constant a and assigns it a value of 50.
The Go language provides a syntax for declaring a set of constants in a single statement block. Take a look at the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Execute [2]
The above code, we declare a set of constants: name, age and country, execution output:
1 2 3 |
|
Constants, as the name implies, are fixed and cannot be reassigned to other values after they are declared. In the code below, we try to assign the value 89 to the constant a again. This is not allowed because a is a constant and the compiler will report an error cannot assign to a.
1 2 3 4 5 6 |
|
Execution[3]
The value of the constant must be determined at compile time, therefore, the function call The returned value cannot be assigned to a constant because the function call occurs at run time.
1 2 3 4 5 6 7 8 9 10 |
|
Execution[4]
In the above code, a is a variable, so it can be passed through the function math.Sqrt(4) Return value initialization.
b 是常量,它的值在编译时就需要确定,而函数 math.Sqrt(4) 的返回值在运行时确定,所以编译会报错:
1 |
|
Go 语言里面,使用双引号括起来来的任何值表示字符串常量,例如:"Hello World", "Sam" 等。
字符串常量属于什么类型呢?答案是类型不确定。
1 |
|
上面的代码,没有为常量 hello 指定类型。
Go 是强类型语言,所有变量的类型都是明确的。
下面的代码,将无类型的常量 n 赋值给变量 name 的机制是怎样的呢?
1 2 3 4 5 6 7 8 9 10 11 12 |
|
执行[5]
未定义类型的常量被使用时,会根据上下文来获得相关类型。
例如上面代码的第 9 行,编译器会自动推断常量 n 的类型为 string。
那有没有创建指定类型的常量呢?
当然是有的,比如下面这样:
1 |
|
typedhello 是 string 类型的常量。
Go 是强类型语言,不同类型之间相互赋值时不允许的。我们看下这个例子:
1 2 3 4 5 6 7 8 9 |
|
执行[6]
上面的代码,我们创建了变量 defaultName 并赋值为常量 Sam,因为 Sam 的默认类型为 string,所以变量 defaultName 的类型也是 string。
接着下一行代码,使用关键字 type 创建了新的类型 myString,底层类型是 string。
接着我们创建了 myString 类型的变量 customName,并将常量 Sam 赋值给它。因为 Sam 未指定类型,所以可以用它给任意字符串变量赋值。
现在变量 defaultName 的类型是 string,变量 customName 的类型是 myString,尽管两者的底层类型相同,但还是不允许相互赋值,所以最后一行代码会编译报错:
1 |
|
布尔常量与字符串常量类似,不过布尔常量只有两个值:true 和 false。字符串常量的使用规则同样适用于布尔常量,就不在这重复说明,来看个例子:
1 2 3 4 5 6 7 8 9 |
|
执行[7]
大家可以自行分析上面的代码。
数值常量包括整型、浮点和复数,数值常量有一些特别的地方,一起看下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
执行[8]
看上面的代码,a 是无类型的常量,值为 5。可能你很想知道 a 的类型到底是什么?为什么它能赋值给不同的类型的变量?
这取决于我们如何使用 a,下面这个例子可以帮助我们理解:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
执行[9]
上面的代码,每个变量的类型取决于各自赋值的常量,5 是整型,5.6 是浮点数,5 + 6i 是复数,所以输出:
1 |
|
理解了这点之后,我们再来看下之前的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
上面的代码中,常量 a 的值是 5,所以 a 表示的数值是比较通用的,它可以表示整型、浮点数甚至是不带虚数的复数,因此它可以分配给任何兼容的类型。常量的类型可以认为是根据上下文动态生成的。比如,var intVar int = a 场景中 a 是 int,var complex64Var complex64 = a 语句中 a 是 复数类型,等等。
可以在表达式中自由组合混用数值类型,并且只有在将它们分配给变量或需要确定类型的地方才需要类型。
1 2 3 4 5 6 7 8 9 10 |
|
执行[10]
上面的代码中,在语法上,5.9 表示 float、8 表示 int,尽管如此,5.9/8 依然是允许的,因为它们都是数值常量。相除的结果是 0.7375,float 类型,因此变量 a 的类型是浮点数。
程序输出:
1 |
|
The above is the detailed content of Go language basics-constants. For more information, please follow other related articles on the PHP Chinese website!