博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A Tour of Go Numeric Constants
阅读量:6884 次
发布时间:2019-06-27

本文共 1479 字,大约阅读时间需要 4 分钟。

Numeric constants are high-precision values.

An untyped constant takes the type needed by its context.

Try printing needInt(Big) too.

 

package main import "fmt"const (    Big = 1 << 100    Small = Big >> 99)func needInt(x int) int {    return x*10 + 1}func needFloat(x float64) float64{    return x * 0.1}func main() {    fmt.Println(needInt(Small))    fmt.Println(needFloat(Small))    fmt.Println(needFloat(Big))}

 

 

 

 

 

package main import "fmt"const (    Big = 1 << 100    Small = Big >> 99)func needInt(x int) int {    return x*10 + 1}func needFloat(x float64) float64{    return x * 0.1}func main() {    fmt.Println(Small);    var intVariable int = 1    //var float32Variable float32 = 1.2    fmt.Println(needInt(Small))    // constant 1267650600228229401496703205376 overflows int    //fmt.Println(needInt(Big))    fmt.Println(needFloat(Small))    fmt.Println(needFloat(Big))    //go语言对类型的要求是很严格的,所以你不能传递int到float中或者float到int    fmt.Println(needInt(intVariable))    //cannot use float32Variable (type float32) as type int in argument to needInt    //fmt.Println(needInt(float32Variable))    //cannot use intVariable (type int) as type float64 in argument to needFloat    //fmt.Println(needFloat(intVariable))    //cannot use float32Variable (type float32) as type float64 in argument to needFloat    //fmt.Println(needFloat(float32Variable))}

不过常量却相对宽容一些

//constant 1267650600228229401496703205376 overflows int    fmt.Println(Big);

 

转载于:https://www.cnblogs.com/ghgyj/p/4052714.html

你可能感兴趣的文章
【原创翻译】布尔值(boolean)
查看>>
关于scrapy的piplines
查看>>
通向架构师的道路(第一天)之Apache整合Tomcat - lifetragedy的专栏 - 博客频道 - CSDN.NET...
查看>>
项目、软件开发过程中版本术语
查看>>
CSS实现背景透明,文字不透明(各浏览器兼容)
查看>>
【转】[大学引导]超级链接、字体颜色、音乐播放公式
查看>>
T-SQL中INSERT、UPDATE
查看>>
Linux下Nginx服务器配置Modsecurity实现Web应用防护系统
查看>>
openSUSE13.2安装ruby和rails
查看>>
python 高级函数
查看>>
F.Cards with Numbers
查看>>
简单入门Buffer
查看>>
OO第四阶段总结
查看>>
javascript总结02
查看>>
创建windows服务
查看>>
HTML5 入门基础
查看>>
C++文件操作(fstream)
查看>>
用main函数传参做简单的计算器的代码
查看>>
python中struct.unpack的用法
查看>>
体绘制(Volume Rendering)概述之4:光线投射算法(Ray Casting)实现流程和代码(基于CPU的实现)...
查看>>