From b5341ac98d5c32ac6dfbf215b1f9c9db838ce5a7 Mon Sep 17 00:00:00 2001 From: Keith Rarick Date: Sun, 4 Nov 2012 19:06:08 -0800 Subject: [PATCH] more about constants --- examples/constants/constants.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/examples/constants/constants.go b/examples/constants/constants.go index 9fca9fb..7ef0a5e 100644 --- a/examples/constants/constants.go +++ b/examples/constants/constants.go @@ -10,4 +10,20 @@ 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 + + // A numeric constant has no type until it's given one, such as by + // an explicit cast. + fmt.Println(int64(d)) + + // A number can also be given a type by using it in a context that + // requires one, such as a variable assignment or funcion call. + // The type it gets depends on its value. + fmt.Println(n) // int + fmt.Println(d) // float64 }