In order to execute code when a package is installed in Go, we can use the init
function. The init
function is executed automatically when the package is initialized.
Here's an example of using the init
function in Go:
main.go142 chars12 lines
In this example, we define a package called mypackage
which has a function called MyFunction
. We also define an init
function which will be executed automatically when the package is initialized.
When we import and use the mypackage
package in our main program, we will see the message "mypackage initialized" printed to the console.
main.go116 chars11 lines
So, whenever the mypackage
package is installed, the init
function will execute automatically and print the message to the console.
gistlibby LogSnag