-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit_and_main.go
52 lines (43 loc) · 1007 Bytes
/
init_and_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
/*
* Date: 2018-11-9
*
* Description:
* Demo to show init() and main() sequence of execution. Below is the order of
* execution:
* 1. Import statements executed.
* 2. All variables and constants gets initialised
* 3. init() gets executed, if there are multiple inits defined, they are
* executed in the order they are writtern.
* 4. main() executed, main can be only one. No multiple definations allowed
* for main()
*/
import "fmt"
var WhatIsThe = AnswerToLife()
func AnswerToLife() int {
fmt.Println("Calling AnswerToLife().")
return 42
}
func init() {
fmt.Println("Calling init().")
WhatIsThe = 1
}
func init() {
fmt.Println("Calling init() - 2.")
WhatIsThe = 0
}
func main() {
fmt.Println("Calling main().")
if WhatIsThe == 0 {
fmt.Println("It's all a lie.")
}
}
/*
* Output:
* ---------------------------
* Calling AnswerToLife().
* Calling init().
* Calling init() - 2.
* Calling main().
* It's all a lie.
*/