On Wednesday, March 22, 2017 at 9:23:04 AM UTC+8, WinD zz wrote:
>
>
> func main() {
>       const abc = 111
>       fmt.Printf("%t\n", abc)
>       fmt.Println(math.Pi * abc)
>       fmt.Printf("%t", math.Pi)
> }
>
> result:
> %!t(int=111)
> 25.132741228718345
> %!t(float64=3.141592653589793)
>
> func main() {
>       const abc int = 111
>       fmt.Printf("%t\n", abc)
>       fmt.Println(math.Pi * abc)
>       fmt.Printf("%t", math.Pi)
> }
>
> result:
> constant 3.14159 truncated to integer
>
> Why when i typed abc int ,the compiler will error.
> But,first example tell me on runtime the abc type is int.
>
> this is why?
>

the first abc is an untyped constant, its default type is int, but it has 
many potential types, such as int64, float64, lfoat32, complex32, etc.
math.Pi is also an untyped constant, its default type is float64, its 
potential types including float64, lfoat32, complex32, etc.
"math.Pi * abc" is also an untyped constant, its default type is float64, 
its potential types including float64, lfoat32, complex32, etc.

the second abc is a typed constant, its type is confirmed as int.
For abc is int type, the second "math.Pi * abc" is expected to be int type 
too. 
But int is not a potential type of math.Pi.
This is why compile error "constant 3.14159 truncated to integer" is output.


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to