防止第三方包的导入

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

Prevent package import for 3rd parties

问题

对于我的当前项目,我正在实现一个代码(单独的包),将其作为二进制文件的一部分,同时也可以被其他项目(二进制文件)导入,就像它是一个库一样。之所以这个包不是共享库的一部分,是因为它的功能与所提到的项目密切相关。

问题是,这个包提供了一个接口和两个实现:

  • 一个直接访问底层数据库的实现,应该只在当前项目中使用
  • 另一个通过HTTP请求进行导入的实现,可以在其他项目中使用(HTTP请求发送到当前项目)

我的问题是:在Go语言中是否有一种机制可以防止某个包或其子包被其他项目导入

这主要是一个美观问题,因为在当前项目之外使用访问数据库的实现将完全无法工作(会抛出错误)。

英文:

For my current project I am implementing a code (separate package) to be part of binary which (this package) can also be imported by other projects (binaries) as if it was a library. The reason why such package is not a part of shared library is that it's functionality is tied tightly with the mentioned project.

The problem is this package provides an interface with two implementations

  • one with direct access to underlying database which should only be used within the current project
  • another with HTTP request to be imported in other projects (HTTP requests going to current project)

My question is: is there a mechanism in Go how to prevent certain part of package or it's sub-package from being imported by another projects?

It's mostly just an aesthetic issue since the DB-accessing implementation won't work at all (throwing errors) when used outside of current project.

答案1

得分: 4

是的,你可以使用内部目录

> 在名为"internal"的目录中或其子目录中的代码只能被根目录为"internal"的目录树中的代码引用。

英文:

Yes, you can use an internal directory.

> Code in or below a directory named "internal" is importable only by code in the directory tree rooted at the parent of "internal".

huangapple
  • 本文由 发表于 2016年11月22日 20:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/40741357.html
匿名

发表评论

匿名网友

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

确定