[英]it is a good practice to put go file on the module path?
I find some project put go file on the module path.我发现一些项目将 go 文件放在模块路径上。 For example:
例如:
core.go
file on the module path and put main.go
on main
directory. core.go
文件放在模块路径上,将main.go
放在main
目录下。main.go
on the module path. main.go
放在模块路径上。 I was wondering if putting go file on the module path is a good practice.我想知道将 go 文件放在模块路径上是否是一个好习惯。
This is largely opinion-based.这主要是基于意见的。 Putting
main.go
into the repository root is often seen with small programs that don't have a lot going on.将
main.go
放入存储库根目录通常会出现在没有很多事情发生的小程序中。 When the main package starts getting a lot of files, what some do is to put tightly coupled code into a subfolder called cmd
and code that can in theory be reused by other projects into a subfolder called pkg
.当主包开始获取大量文件时,一些人所做的是将紧密耦合的代码放入一个名为
cmd
的子文件夹中,并将理论上可以被其他项目重用的代码放入名为pkg
的子文件夹中。 At that point though, the code in pkg
might as well have its own repository.不过,此时
pkg
的代码也可能拥有自己的存储库。
Putting the main package into a subfolder, while still having another package in the repository root, can have various reasons:将主包放入子文件夹,同时在存储库根目录中还有另一个包,可能有多种原因:
main
in a specific manner;main
构建的二进制文件; ie a hierarchical folder/package structure where code that tends to get called later during project execution is placed further down the folder tree. As a rule of thumb though, it is a good idea to name things so that people don't have to specify an import alias to import a package.不过,根据经验,命名事物是个好主意,这样人们就不必指定导入别名来导入包。 Eg don't put a package
foo
in a folder called bar
.例如,不要将包
foo
放在名为bar
的文件夹中。 Naming a module in a manner that one can copy into a browser's address bar to get right to it's repository overview is also a practice I have seen being welcome among people.以一种可以复制到浏览器地址栏以直接访问其存储库概述的方式命名模块也是我看到人们欢迎的一种做法。
Package import paths in Go have many similarities to what's known as namespaces in other languages. Go 中的包导入路径与其他语言中的命名空间有很多相似之处。 Over time, for better or worse, developers and languages alike have begun using this mechanism for more than just avoiding lexical name collisions.
随着时间的推移,无论好坏,开发人员和语言都开始使用这种机制不仅仅是为了避免词法名称冲突。 Developers will often attempt to achieve a logical, hierarchical folder structure within which code is placed.
开发人员通常会尝试实现代码所在的逻辑、分层文件夹结构。 Languages extensively use it to limit visibility of methods and fields.
语言广泛使用它来限制方法和字段的可见性。 As you might guess, what lies ahead is a heated opinion-based discussion unsuited for Stackoverflow.
正如您可能猜到的那样,接下来是一场不适合 Stackoverflow 的基于意见的激烈讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.