feitian124/go-starter

建议不要使用 pkg 目录了

Closed this issue · 5 comments

建议不要使用 pkg 目录了

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

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 中。

之后更新一下文档说明。

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

👍 这个有说服力