-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05-constant.go
63 lines (51 loc) · 1.42 KB
/
05-constant.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @Author: Barnabas Makonda
* @Date: 2019-01-11 15:52:36
* @Last Modified by: Barnabas Makonda
* @Last Modified time: 2019-01-11 16:23:36
*/
package main
import "fmt"
/*
* Constant is an expression that is know during compile time.
* It hold value that was declared and assigned to it all tne time as long as program is running.
* The type is inferred from the right hand side of the expression.
*
*
*/
func main() {
// You can assign single constant a value. Type is inferred from the right side.
// For this case compiler see "John" as string and hence name become of type string.
const name = "John"
// You can create multiple constants at once
const (
numberOne = 1
msg = "Jambo"
)
/* iota can be used to generate set of related constant but distict. can be used to represent
* property that has several distinct possible values. Eg. Days of the week, Month of the years
* They are like enumerated(enum)type from other language.
*/
type DaysOfTheWeek int
type Grades int
// you will only have to specify type once for constants and the rest will be assigned
// unique value with the same datatype as the one assigned to the first one.
const (
Monday DaysOfTheWeek = iota
Tuesday
Wednesday
Thursday
Friday
)
const (
A Grades = iota
B
C
D
E
F
)
fmt.Println(name, numberOne, msg)
fmt.Println(Monday, Tuesday, Wednesday, Thursday, Friday)
fmt.Println(A, B, C, D, E, F)
}