标签:pre font ons color highlight const not 常量 ack
常量就是那些不可以改变的值
Go语言中定义常量的方式: const a = "admin"
则 a 就是一个常量,但实际上其后面的 "admin" 也是一个字符串常量。
此时的常量 a 的值不能被改变,如果继续给a赋值,go会报错:cannot assign to a
还有一点,常量的值必须是编译的时候就要确定下来,不能在程序运行时计算。例如:
package main
import (
"fmt"
)
func main() {
a := 10
const b = a * 3
fmt.Println("b =", b);
}
go会报错: const initializer a * 3 is not a constant
标签:pre font ons color highlight const not 常量 ack
原文地址:https://www.cnblogs.com/hatsusakana/p/9810077.html