-
go基础教程之Go常量实例
本站最新发布 go从入门到精通|go基础教程
试听地址 https://www.xin3721.com/eschool/go.html
试听地址 https://www.xin3721.com/eschool/go.html
Go语言支持字符,字符串,布尔和数值的常量。
所有的示例代码,都放在
F:\worksp\golang
目录下。安装Go编程环境请参考:http://www.yiibai.com/go/go_environment.html
const
关键字用来声明一个常量值。const
语句可以出现在var
语句的任何地方。常量表达式以任意精度执行算术运算。数字常量在给定一个数字常量之前不指定类型,例如通过显式转换。
可以通过在需要一个数字的上下文中使用它来给予数字一个类型,例如,变量赋值或函数调用。 例如,这里math.Sin
期望一个float64
类型。
variables.go
的完整代码如下所示 -
package main
import "fmt"
import "math"
// `const` declares a constant value.
const s string = "constant"
func main() {
fmt.Println(s)
// A `const` statement can appear anywhere a `var`
// statement can.
const n = 500000000
// Constant expressions perform arithmetic with
// arbitrary precision.
const d = 3e20 / n
fmt.Println(d)
// A numeric constant has no type until it's given
// one, such as by an explicit cast.
fmt.Println(int64(d))
// A number can be given a type by using it in a
// context that requires one, such as a variable
// assignment or function call. For example, here
// `math.Sin` expects a `float64`.
fmt.Println(math.Sin(n))
}
执行上面代码,将得到以下输出结果 -
F:\worksp\golang>go run constants.go
constant
6e+11
600000000000
-0.28470407323754404
//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/go/golang-constants.html
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能