use a function-call example for coercing an apparent int to a float. ref #27
This commit is contained in:
parent
363613d951
commit
39abea3d12
@ -4,6 +4,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
import "math"
|
||||||
|
|
||||||
// `const` declares a constant value.
|
// `const` declares a constant value.
|
||||||
const s string = "constant"
|
const s string = "constant"
|
||||||
@ -18,15 +19,15 @@ func main() {
|
|||||||
// Constant expressions perform arithmetic with
|
// Constant expressions perform arithmetic with
|
||||||
// arbitrary precision.
|
// arbitrary precision.
|
||||||
const d = 3e20 / n
|
const d = 3e20 / n
|
||||||
|
fmt.Println(d)
|
||||||
|
|
||||||
// A numeric constant has no type until it's given
|
// A numeric constant has no type until it's given
|
||||||
// one, such as by an explicit cast.
|
// one, such as by an explicit cast.
|
||||||
fmt.Println(int64(d))
|
fmt.Println(int64(d))
|
||||||
|
|
||||||
// A number can also be given a type by using it in a
|
// A number can be given a type by using it in a
|
||||||
// context that requires one, such as a variable
|
// context that requires one, such as a variable
|
||||||
// assignment or function call. The type it gets
|
// assignment or function call. For example, here
|
||||||
// depends on its value.
|
// `math.Sin` expects a `float64`.
|
||||||
fmt.Println(n) // int
|
fmt.Println(math.Sin(n))
|
||||||
fmt.Println(d) // float64
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user