Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

init() not running in new package

Tags:

go

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?

like image 495
mark Avatar asked Jan 27 '17 05:01

mark


1 Answers

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).

like image 137
VonC Avatar answered Nov 07 '22 04:11

VonC