go--入门学习(三)

在Go语言中,const关键字用于定义常量。常量是指在程序运行期间不会改变的值。可以使用const来定义数值、字符串、布尔值或字符类型的常量。

const pi = 3.14
const greeting = "Hello, World!"
const isActive = true
const a = 'A'

常量的作用

  • 常量在编译时就确定了值,这使得程序更高效。
  • 常量的值不能在运行时被修改。
package main

import "fmt"

const pi = 3.14

func main() {
    fmt.Println("The value of pi is:", pi)
}

特殊的iota常量生成器

iota是Go语言的常量计数器,用于生成一组相关的常量值,从0开始,每定义一个常量自动递增1。

const (
    a = iota // 0
    b        // 1
    c        // 2
)


 

猜你喜欢

转载自blog.csdn.net/weixin_63566388/article/details/141717996
今日推荐