是的,可以只从一个包中导入一个函数。

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

Is it possible to import only a function from a package?

问题

有时候我只需要一个包中的一个函数,所以导入整个包似乎不太明智。因此,问题是:是否可以只导入一个函数?

英文:

Sometimes I need only a function from a package so importing the whole package doesn't seem performance wise. Thus the question: Is it possible to import only a function ?

答案1

得分: 30

  1. 不,这是不可能的。
  2. 不,这对性能没有影响。包中未使用的内容应该会被链接器删除,因此不会使你的二进制文件变得混乱。
英文:
  1. No, this is not possible.

  2. No, this has no impact on performance. Unused stuff from a package should be dropped by the linker so it won't clutter your binary.

答案2

得分: 2

同意@Volker的观点,这是不可能的,而且也不会使你的二进制文件变得混乱。如果你仍然坚持只使用该软件包,还有一些进一步的考虑事项,那就是该软件包可以被Fork。而如果是来自Go的库,源代码是完全可见的,你可以从中获取很多关于如何实现自己等效函数的想法。

英文:

Agree with @Volker that its not possible and that it would not clutter your binary. Some further considerations if you are still keep on only having the package are that the package could be Forked. Whereas if its a library from Go the source code is all available for view and you can get plenty of ideas about how you can implement you own equivalent function.

huangapple
  • 本文由 发表于 2014年4月14日 17:16:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/23056297.html
匿名

发表评论

匿名网友

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

确定