I am trying to write a boiler plate code - where the use case is following. Consider the following as my directory structure:
sample-service
- migrations
- migration.go
- main.go
In main.go I want to write a logic which will import the migration package if the directory or the files exist.
After importing the package I want to call the function/methods of that package.
I have tried using plugins but that would restrict me with Operating System.
I am not sure if go/importer or reflect can work, i tried few approaches but none worked.
The only way to import a package is to write an import statement. Imports happen during the build of the program, so you can't change them at runtime.
If plugins are not an option you can have the migrations package register itself somewhere (other than the main package because that cannot be imported).
For instance:
Consider generating the init function in the migrations package automatically during the build. This can be as simple as: