September 2017
Intermediate to advanced
466 pages
9h 33m
English
Every Go package can have a function named init() that is automatically executed at the beginning of the execution. So, let's add the following init() function to the code of the anotherPackage.go package:
func init() {
fmt.Println("The init function of anotherPackage")
}
The current implementation of the init() function is naive and does nothing special. However, there are times when you want to perform important initializations before you start using a package such as opening database and network connections: in these relatively rare cases the init() function is invaluable.
Read now
Unlock full access