zoukankan      html  css  js  c++  java
  • A Tour of Go Numeric Constants

    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);
  • 相关阅读:
    江の島西浦写真館2-1
    江の島西浦写真館1-2
    Oracle 查询表空间使用情况
    Oracle 的开窗函数 rank,dense_rank,row_number
    oracle11G 用户密码180天修改概要文件过程
    CentOS6 安装 MySQL5.7
    linux下SS 网络命令详解
    CentOS6 网络设置
    redhat 6 红帽6 Linux 网络配置
    Oracle分析函数——函数列表
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4052714.html
Copyright © 2011-2022 走看看