相同项目的不同文件夹中具有相同包名

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

Identical package names in different folders for same project

问题

我正在进行一个大型项目,可能会包含数万行代码,对于当前的结构,我是这样做的:

main.go
controllers/NAME.go
models/NAME.go

问题在于controllers和models目录包含了很多文件,都使用了package controllerspackage models。因此,我考虑将其拆分如下:

main.go
controllers/user/NAME.go
models/user/NAME.go

在controllers包中,user文件可能包含routes.goprofile.go等文件。

现在,我了解到将包命名为controllersUsercontrollers_user是不好的做法,但我担心将它们都命名为package user可能也不是一个好主意,因为它们是同一个项目的一部分(即使它们位于不同的目录中)。

在我的情况下,你会推荐使用什么样的命名结构?

我意识到位于controllers/user目录中的文件即使使用相同的package user名称(除非我导入),也无法与models/user进行交互。我也知道我可以轻松地将它们都导入为userControllers "controllers/user"userModels "models/user",但我想知道这样做是否是一种不好的做法?

英文:

I'm working on a big project which will likely end up containing tens of thousands of line of code, for the current structure I do like this:

main.go
controllers/NAME.go
models/NAME.go

The problem with that is that the controllers and models directory contains a lot of files, all using package controllers and package models. I was therefore thinking about splitting it up like this:

main.go
controllers/user/NAME.go
models/user/NAME.go

Where the user files in the controllers package might contain files like routes.go, profile.go, etc.

Now, I read that it's bad practice to name packages like controllersUser or controllers_user, but I worry it might be a bad idea to name both as package user since they're a part of the same project (even if they're located in different directories).

What kind of naming structure would you recommend given my situation?

I realize that the files located in controllers/user will not be able to interact with models/user even through they share the same package user name (unless I import of course), and I also know that I can easy import both as userControllers "controllers/user" and userModels "models/user", but I wonder if it's a bad practice to do like this?

答案1

得分: 6

我建议你阅读《Go包的样式指南》(https://rakyll.org/style-packages/)。这是一篇很棒的文章,包含了我在Go项目布局方面找到的最好的建议。不过,对于你的具体情况,我很难给出一个命名建议,因为它太过于依赖于你的系统功能的种类。有时候,我会将其视为“应用程序的每个部分一个包”。但我理解我的描述可能过于个人化,不太有帮助。

英文:

I suggest you read style guideline for Go packages. It's a fantastic read and contains the best advice I found on the subject when it comes to project layout in Go. I'm having hard times suggesting you a naming for your specific case though because it depends too much on the kind of functionality your system has. Sometimes, I think of it as a "package per part of the application". But I understand my description may result too personal and not very helpful.

huangapple
  • 本文由 发表于 2017年4月8日 23:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/43295691.html
匿名

发表评论

匿名网友

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

确定