The Go language reserve two functions for special purpose and the functions are main() and init() function.
In Go language, the main package is a special package which is used with the programs that are executable and this package contains main() function. The main() function is a special type of function and it is the entry point of the executable programs. It does not take any argument nor return anything. Go automatically call main() function, so there is no need to call main() function explicitly and every executable program must contain single main package and main() function.
Sorted slice: [2 5 10 76 78 123 345 567] Index value: 3 Time: 1257894000
init() function is just like the main function, does not take any argument nor return anything. This function is present in every package and this function is called when the package is initialized. This function is declared implicitly, so you cannot reference it from anywhere and you are allowed to create multiple init() function in the same program and they execute in the order they are created. You are allowed to create init() function anywhere in the program and they are called in lexical file name order (Alphabetical Order). And allowed to put statements if the init() function, but always remember to init() function is executed before the main() function call, so it does not depend to main() function. The main purpose of the init() function is to initialize the global variables that cannot be initialized in the global context.
Welcome to init() function Hello! init() function Welcome to main() function
- Function Arguments in Golang
- Function as a Field in Golang Structure
- How to pass a Slice to Function in Golang?
- How to pass an Array to a Function in Golang?
- Golang Pointer to an Array as Function Argument
- Zero value in Golang
- Channel in Golang
- Methods With Same Name in Golang
- Pointers in Golang
- Structures in Golang
- Encapsulation in Golang
- Strings in Golang
- Recover in Golang
- Methods in Golang
- Slices in Golang
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.