Home >Backend Development >Golang >Go language basics-constants

Go language basics-constants

Go语言进阶学习
Go语言进阶学习forward
2023-07-21 11:05:521438browse

What is a constant?

Constants are used in Go language to represent fixed values, for example:

95
"I love Go"
67.89

Declare constants

The keyword const is used to declare a constant. Let’s take a look at how to declare constants in Go language:

package main

import (
    "fmt"
)

func main() {
    const a = 50
    fmt.Println(a)
}

Execution[1]

The above code declares the constant a and assigns it a value of 50.

Declare a set of variables

The Go language provides a syntax for declaring a set of constants in a single statement block. Take a look at the following example:

package main

import (
    "fmt"
)

func main() {
    const (
        name = "John"
        age = 50
        country = "Canada"
    )
    fmt.Println(name)
    fmt.Println(age)
    fmt.Println(country)

}

Execute [2]

The above code, we declare a set of constants: name, age and country, execution output:

John
50
Canada

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.

package main

func main() {
    const a = 55 //allowed
    a = 89 //reassignment not allowed
}

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.

package main

import (
    "math"
)

func main() {
    var a = math.Sqrt(4)   //allowed
    const b = math.Sqrt(4) //not allowed
}

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) 的返回值在运行时确定,所以编译会报错:

./prog.go:9:8: const initializer math.Sqrt(4) is not a constant

字符串常量、类型确定常量和无类型常量

Go 语言里面,使用双引号括起来来的任何值表示字符串常量,例如:"Hello World", "Sam" 等。

字符串常量属于什么类型呢?答案是类型不确定

const hello = "Hello World"

上面的代码,没有为常量 hello 指定类型。

Go 是强类型语言,所有变量的类型都是明确的。

下面的代码,将无类型的常量 n 赋值给变量 name 的机制是怎样的呢?

package main

import (
    "fmt"
)

func main() {
    const n = "Sam"
    var name = n
    fmt.Printf("type %T value %v", name, name)

}

执行[5]

未定义类型的常量被使用时,会根据上下文来获得相关类型。

例如上面代码的第 9 行,编译器会自动推断常量 n 的类型为 string。

那有没有创建指定类型的常量呢?

当然是有的,比如下面这样:

const typedhello string = "Hello World"

typedhello 是 string 类型的常量。

Go 是强类型语言,不同类型之间相互赋值时不允许的。我们看下这个例子:

package main

func main() {
        var defaultName = "Sam" //allowed
        type myString string
        var customName myString = "Sam" //allowed
        customName = defaultName //not allowed

}

执行[6]

上面的代码,我们创建了变量 defaultName 并赋值为常量 Sam,因为 Sam 的默认类型为 string,所以变量 defaultName 的类型也是 string。

接着下一行代码,使用关键字 type 创建了新的类型 myString,底层类型是 string。

接着我们创建了 myString 类型的变量 customName,并将常量 Sam 赋值给它。因为 Sam 未指定类型,所以可以用它给任意字符串变量赋值。

现在变量 defaultName 的类型是 string,变量 customName 的类型是 myString,尽管两者的底层类型相同,但还是不允许相互赋值,所以最后一行代码会编译报错:

./prog.go:7:20: cannot use defaultName (type string) as type myString in assignment

布尔常量

布尔常量与字符串常量类似,不过布尔常量只有两个值:true 和 false。字符串常量的使用规则同样适用于布尔常量,就不在这重复说明,来看个例子:

package main

func main() {
    const trueConst = true
    type myBool bool
    var defaultBool = trueConst //allowed
    var customBool myBool = trueConst //allowed
    defaultBool = customBool //not allowed
}

执行[7]

大家可以自行分析上面的代码。

数值常量

数值常量包括整型、浮点和复数,数值常量有一些特别的地方,一起看下:

package main

import (
    "fmt"
)

func main() {
    const a = 5
    var intVar int = a
    var int32Var int32 = a
    var float64Var float64 = a
    var complex64Var complex64 = a
    fmt.Println("intVar",intVar, "\nint32Var", int32Var, "\nfloat64Var", float64Var, "\ncomplex64Var",complex64Var)
}

执行[8]

看上面的代码,a 是无类型的常量,值为 5。可能你很想知道 a 的类型到底是什么?为什么它能赋值给不同的类型的变量?

这取决于我们如何使用 a,下面这个例子可以帮助我们理解:

package main

import (
    "fmt"
)

func main() {
    var i = 5
    var f = 5.6
    var c = 5 + 6i
    fmt.Printf("i's type is %T, f's type is %T, c's type is %T", i, f, c)

}

执行[9]

上面的代码,每个变量的类型取决于各自赋值的常量,5 是整型,5.6 是浮点数,5 + 6i 是复数,所以输出:

i's type is int, f's type is float64, c's type is complex128

理解了这点之后,我们再来看下之前的代码:

package main

import (
    "fmt"
)

func main() {
    const a = 5
    var intVar int = a
    var int32Var int32 = a
    var float64Var float64 = a
    var complex64Var complex64 = a
    fmt.Println("intVar",intVar, "\nint32Var", int32Var, "\nfloat64Var", float64Var, "\ncomplex64Var",complex64Var)
}

上面的代码中,常量 a 的值是 5,所以 a 表示的数值是比较通用的,它可以表示整型、浮点数甚至是不带虚数的复数,因此它可以分配给任何兼容的类型。常量的类型可以认为是根据上下文动态生成的。比如,var intVar int = a 场景中 a 是 int,var complex64Var complex64 = a 语句中 a 是 复数类型,等等。

数值表达式

可以在表达式中自由组合混用数值类型,并且只有在将它们分配给变量或需要确定类型的地方才需要类型。

package main

import (
    "fmt"
)

func main() {
    var a = 5.9 / 8
    fmt.Printf("a's type is %T and value is %v", a, a)
}

执行[10]

上面的代码中,在语法上,5.9 表示 float、8 表示 int,尽管如此,5.9/8 依然是允许的,因为它们都是数值常量。相除的结果是 0.7375,float 类型,因此变量 a 的类型是浮点数。

程序输出:

a's type is float64 and value is 0.7375

The above is the detailed content of Go language basics-constants. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete