package main import "fmt" const Pi = 3.14 func main() { const World = "世界" fmt.Println("Hello", World) fmt.Println("Happy", Pi, "Day") const Truth = true fmt.Println("Go rules?", Truth) }
数值常量
数值常量是高精度数值。 常量虽然没有指定类型,却可以根据实际情况采用合适类型,保证精度够用。
试试输出 needInt(Big) :
package main import "fmt" const ( // Create a huge number by shifting a 1 bit left 100 places. // In other worlds, the binary number that is 1 followed by 100 zeros. Big = 1 << 100 // Shift it right again 99 places, so we end up with 1<<1, or 2. 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)) }
类型
到这为止,我们介绍的常量申明方式是无类型的( untyped )
特别声明:以上文章内容仅代表作者本人观点,不代表变化吧观点或立场。如有关于作品内容、版权或其它问题请于作品发表后的30日内与变化吧联系。
- 赞助本站
- 微信扫一扫
-
- 加入Q群
- QQ扫一扫
-
评论