英文:
Conditional imports golang
问题
我对Go语言还不太熟悉,我正在努力学习如何在只有存在时导入一个包。有没有办法做到这一点?我尝试在包名前面加上"_",但是如果包不存在,程序就无法编译。我知道我可能走错了路,但是在我只想在包存在时导入它的情况下,还有其他方法吗?
英文:
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?
答案1
得分: 6
这在Go语言中是不可能的。作为一种静态编译语言,只有在所有依赖项存在的情况下才能编译二进制文件。
你能否提供更多关于为什么你需要这个的细节呢?也许有一种更简单或更符合惯用方式来实现你的想法。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论