• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ATourofGoNumericConstants

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何让你的项目同时支持go vendor和go module发布时间:2022-07-10
下一篇:
相关性知识成就了Alpha Go Zero发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap