-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
37 lines (27 loc) · 925 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package constants
import (
"fmt"
"math"
"github.com/symonk/learning-golang/utils"
)
// consts can be used outside of function scope, but cannot be dynamically computed
const s string = "foo"
func Run() {
fmt.Println(s)
// const can be used inside functions
const n = 250
fmt.Println(n)
// constant expressions can perform arithmetic with arbitrary precision
const d = 3e20 / n
fmt.Println(d)
// Numeric constants do not have a type until they are given one
fmt.Println(int64(d))
// Numbers can be a type by using them in a context that requires one
// Such as variable assignment or functioin call. For example
// Math.Sin requires a float64:
utils.PrintValueAndType(n) // n is currently an integer
fmt.Println(math.Sin(n)) // n can be used as a float64
anotherInt := 100
utils.PrintValueAndType(anotherInt)
math.Sin(float64(anotherInt)) // Note: This must be cast as it is not a constant!
}