相同项目的不同文件夹中的相同包名称 [英] Identical package names in different folders for same project

查看:236
本文介绍了相同项目的不同文件夹中的相同包名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$ p $我正在研究一个可能最终包含数万行代码的大项目,因为目前的结构我喜欢这样做: p> main.go
controllers / NAME.go
models / NAME.go

问题在于controllers和models目录包含大量文件,全部使用程序包控制器程序包模型。因此,我想这样拆分它:

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

控制器包中的用户文件可能包含文件如 routes.go profile.go 等。



现在,我读到名称为 controllersUser controllers_user 的软件包是不好的做法,但我担心它可能是因为它们是同一个项目的一部分(即使它们位于不同的目录中),所以它们都被命名为 package user



根据我的情况,你会推荐什么样的命名结构?

我意识到位于控制器/用户中的文件将无法使用即使通过它们共享相同的包用户名名称(除非我导入当然),也可以与模型/用户交互,而且我也知道我可以轻松地将 userControllerscontrolle rs / user userModelsmodels / user,但我不知道这样做是不是一个坏习惯?

解决方案

我建议您阅读 style Go包的指南。这是一个很棒的阅读,包含了我在Go上关于项目布局时发现的最佳建议。我有困难的时候建议你为你的具体情况命名,但因为它太依赖于你系统的功能。有时,我认为它是应用程序的每个部分的包。但我知道我的描述可能会导致过于个人化,并且不是很有用。


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?

解决方案

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.

这篇关于相同项目的不同文件夹中的相同包名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆