如何构建go命令的内部结构

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

How to structure go command internals

问题

如果你正在编写一个大型的go命令(而不是一个包),你会如何布局它的内部结构?我希望有一些实用的包等,但除了命令的代码之外,不要将它们暴露给其他任何人。如果它们都在同一个仓库中,我不明白有什么阻止别人导入这些内部使用的包。

英文:

If your writing a go command (not a package) that is large how do you layout its internals? I would love to have some utility packages etc but not have those exposed for anything except the command's code. If it's all in the same repo I don't see whats stopping someone from importing those internal use packages.

答案1

得分: 1

答案很简单(但很可能不是你想听到的):做任何你喜欢且被允许的事情。将实用程序包放在某个地方是完全可以的,可以放在与“子包”相同的存储库中,也可以放在不同的存储库中。

关于

我不明白有什么阻止别人导入这些内部使用的包

你为什么在意呢?如果这些包封装了独特且可用的功能,别人可能会从导入它们中受益。将这些函数与命令放在一起并不能“保护”它们,意思是“没有人应该能够查看我的代码”。只需在文档中明确指出,你随时可以进行不兼容的更改。Go语言并不是为了防止每一个愚蠢的行为,也不是为了保护那些故意忽略任何文档的懒惰和无能的人们。

英文:

The answer is simple (but most probably not what you'd like to hear): Do whatever you like and is allowed. Having utility packages living somewhere is perfectly fine, either in the same repo as a "sub-package" or in a different repo.

Regarding

> I don't see whats stopping someone from importing those internal use packages

Why do you care? If the packages encapsulate distinct and usable functionalities someone might benefit from importing them. Keeping those function together with the command does not "protect" them in the sense of "nobody should be able to look at my code". Just make it clear in the docs that you'll feel free to do incompatible changes at any time. Go is not about preventing each and any stupidity and protecting the lazy and incapable ones who deliberately ignore any documentation from shooting their foot.

答案2

得分: 1

你可以将你的实用程序包设置为包内部,像这样。这样做不会阻止任何人使用它们,但是 a) 你为什么要在意呢?b) 包的结构会清楚地提示这些“子包”是项目内部使用的。

英文:

You might make your utility packages internal to your package — like this. This won't prevent anyone from using them but a) why would you care anyway? b) the package's structure would give a clear hint on that these "subpackages" are internal to the project.

huangapple
  • 本文由 发表于 2014年1月8日 09:19:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/20985308.html
匿名

发表评论

匿名网友

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

确定