I haven't used Go in a while and I'm just starting to work on an old project again.
I have init()
functions in a number of the packages and they work fine. However I've just created a new package and added an init()
function but it won't run during initialization like the others. If I put the init()
function in an previously existing package it runs fine...
I believe this is a simple problem but I can't for the life of me figure it out. What could I be doing wrong?
If you main program does not import your new package at all... its init()
function would not be called.
If you just want an imported package's init()
function to be executed, and don't want to use package's other content, you should modify import "foo"
to import _ "foo"
.
See init
function (and its full documentation in program execution).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With