Reputation: 91
I am quite new with Go, and I am struggling to import a package only if it exists. Is there a way to do this? I tried with "_" before the package name, but if the package does not exist the program won't compile. I am aware that I could be on the wrong road, but how else should I treat this case in which I want to import a package only if it exists?
Upvotes: 5
Views: 6657
Reputation: 9541
This is not possible with Go. As a statically compiled language you can only compile a binary if all its dependencies exist. Can you give us some more details on why you need this in the first place? Maybe there is a simpler or more idiomatic way to achieve your idea.
Upvotes: 6