当前位置: 首页 > news >正文

go const(常量)

常量介绍

在这里插入图片描述

示例

package mainimport ("fmt"
)func main() {const name = "tom"fmt.Println(name)const tax float64 = 0.8fmt.Println(tax)
}
go run const.go 
tom
0.8
package mainimport ("fmt"
)func main() {const a intfmt.Println(a)
}
go run const.go 
# command-line-arguments
./const.go:8:8: missing init expr for a
package mainimport ("fmt"
)func getVal() {fmt.Printf("测试")
}
func main() {const b = 9 / 3fmt.Println(b)//const c = getVal()//fmt.Println(c)
}
go run const.go 
3
package mainimport ("fmt"
)func getVal() {fmt.Printf("测试")
}
func main() {//const b = 9 / 3//fmt.Println(b)const c = getVal()fmt.Println(c)
}
go run const.go 
# command-line-arguments
./const.go:13:12: getVal() (no value) used as value
package mainimport ("fmt"
)func getVal() {fmt.Printf("测试")
}
func main() {//const b = 9 / 3//fmt.Println(b)//const c = getVal()//fmt.Println(c)num := 9const b = num / 3fmt.Println(b)
}
go run const.go 
# command-line-arguments
./const.go:16:12: num / 3 (value of type int) is not constant

常量比较简单的写法

package mainimport ("fmt"
)func getVal() {fmt.Printf("测试")
}
func main() {const (a = 1b = 2)fmt.Println(a, b)const (c = iotade)fmt.Println(c, d, e)
}
go run const.go 
1 2
0 1 2

http://www.mrgr.cn/news/9496.html

相关文章:

  • 零基础5分钟上手亚马逊云科技核心云架构知识-创建NoSQL数据库
  • Bootstrap、Ant Design、Element UI特点和优势
  • React学习笔记(三)——redux状态管理工具
  • Web-ssrfme--redis 未授权访问攻击
  • flink周边使用技巧与汇总
  • ES6笔记总结(Xmind格式):第三天
  • vue打包设置 自定义的NODE_ENV
  • StringRedisTemplate 删除某key开头的
  • 多个程序监听不同网卡的相同端口、相同网卡不同IP的相同端口
  • 【开发工具】Maven打包跳过Test命令详解:POM配置、IntelliJ IDEA集成与最佳实践
  • 企业高性能web服务器(nginx)
  • 学习前端面试知识(16)
  • 大数据-100 Spark 集群 Spark Streaming DStream转换 黑名单过滤的三种实现方式
  • 【消息中间件】RabbitMQ
  • 人话学Python-元组
  • Java 选择排序算法
  • 微信小程序wxml与标准的html的异同
  • 5G+AI智慧校园解决方案
  • Vue 项目实战1-学习计划表
  • 安全可靠的国产自研数据库PolarDB V2.0,让数据库开发像“搭积木”一样简单