go 常量定义和使用

it2022-05-05  161

常量的定义与变量类似,只不过使用 const 关键字。

常量可以是字符、字符串、布尔或数字类型的值。

常量不能使用 := 语法定义。

常量必须定义时赋值,不能多次赋值

 

 

 

package main import "fmt" const PI = 3.1415926 const ErrorCode = "001" const SuccessCode = "200" const Open = true const Close = false func main() {     const state = "success"     fmt.Println(PI, Open, state)     //state = "closed" 报错,只能初始化一次     //fmt.Println(state)     for i := 0; i < 5; i++ {         const s = 6         fmt.Println(s)     } }  

go语言开发交流qq群 857263711

保持进步希望每个人都能找到自己喜欢的方式生活、工作。

转载于:https://www.cnblogs.com/songhuan999999/p/11187728.html


最新回复(0)