2017-03-31 19:23
浏览 177

Golang。 从包中仅导入一个文件

Hi all. I am new in GO and need some help. I've got a project with this structure

GO Project structure

But let's pretend that I have 100500 items in my logic package. What if I need import only one or two files for the specific package. Can I do it or I only can import a full package?

图片转代码服务由CSDN问答提供 功能建议

大家好。 我是GO新手,需要一些帮助。 我有一个具有这种结构的项目


但我们假设我有100500个项目 在我的逻辑包中。 如果我只需要为特定程序包导入一个或两个文件,该怎么办。 我可以这样做还是只能导入一个完整的程序包?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsdzvprlp51692469 2017-03-31 19:28

    No, in Go you import packages, not files. However, the compiler generally only includes in the compiled binary functions and types actually referenced, so even if you include a massive package in your import (which is discouraged anyway), they'll not usually be included in the final binary unless needed.

    And as RayfenWindspear pointed out, if a package is large enough that you want to only import a file or two, that's probably a pretty good smell test that you need to refactor that package.

    打赏 评论
  • doutu9810 2017-04-01 04:34

    I realize this question is largely already answered. But here are my thoughts anyways.

    If the specific files can work independently from the rest of the package or do so with some minor modifications you can just simply copy those files to the project you want to use them in. Then reference the functions/methods/etc directly. But if you are using parts of a big package in various projects it would definitely be more suitable to break it into smaller packages. Then you can use those parts independently in as many projects as you wish without the excess baggage.

    打赏 评论

相关推荐 更多相似问题