Conditional imports golang

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2017年6月26日 19:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/44759016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定