Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议不要使用 pkg 目录了 #1

Closed
hysios opened this issue Jun 22, 2021 · 5 comments
Closed

建议不要使用 pkg 目录了 #1

hysios opened this issue Jun 22, 2021 · 5 comments

Comments

@hysios
Copy link

hysios commented Jun 22, 2021

No description provided.

@feitian124
Copy link
Owner

feitian124 commented Jun 22, 2021

why?
查阅来一些资料, 如 https://github.com/golang-standards/project-layout.git,
他们建议 pkg 放公开的包, internal 放私有的包

@feitian124
Copy link
Owner

thanks

golang-standards/project-layout#10
这个帖子持反对意见, 主要理由是除了 internal 有编译器保护, 其他都默认是可以 import 的,天生的 public, 放不放 pkg 无所谓。为了路径更简短等原因,他倾向于不使用 pkg 目录。

https://github.com/golang-standards/project-layout#pkg
The /pkg directory is still a good way to explicitly communicate that the code in that directory is safe for use by others.
他说 pkg 仍然是个不错的约定,pkg 中的包是可以被其他人安全使用的。基本是持肯定态度, 但不强烈。

https://travisjeffery.com/b/2019/11/i-ll-take-pkg-over-internal/
在末尾他说 With that said: I’ve begun using pkg. Trying it out. pkg is consistent with cmd and internal and other non-package directories.
建议使用, 且比2还要强烈的态度。

综合来看, pkg 是延续 go1 来的一个比较老派的做法, 社区也出现了一些声音认为不需要 pkg 包, 目前还没有强烈的共识。
我觉得可以将评级从 推荐 降低到 可以使用:

  • 如果喜欢路径更短, 可以不放在 pkg 中
  • 如果想一级目录中的文件较干净清爽,可以放入 pkg 中。

之后更新一下文档说明。

@hysios
Copy link
Author

hysios commented Jun 23, 2021

golang-standards/project-layout#117
好不容易才找到 rsc 的说法,standards/projects-layout 大多情况下,都是可取的,但是在实践当中并不会太多使用到 pkg 这一目录,保持简单一直是使用的 golang 开发遵循的目标。

@feitian124
Copy link
Owner

👍 这个有说服力

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants