英文:
Project structure for a tool with multiple UIs
问题
我正在使用golang玩耍,并制作了一个用于生成密码的工具。最初它是作为一个命令行工具来使用的。后来,我将生成逻辑分离到一个单独的包中(仍然是同一个GitHub仓库),并将主函数留在项目的根目录中。现在,我还想添加一个简单的Web前端(不需要太复杂),但我不知道如何组织包的结构。
我应该将命令行入口点和Web界面都放在同一个项目的不同包中(这样根目录就为空)。或者也许我应该将实际的生成库移动到根目录,将UI放在单独的包中。我猜另一个选项是将UI放在GitHub上的单独项目中,但它们只会用于这个库,所以这似乎不是一个好主意。
我记得在一些项目中看到过命名为cmd的包,但从未遇到过有多个前端的项目。有没有一种go(gettable)的方法来做到这一点?
英文:
I'm playing with golang and made a tool for password generation. Initially it was intended to be used as a command line tool. I later separated the generation logic into a separate package (still same github repository) and left the main function in the root of the project. Now I also want to add a simple web frontend (nothing fancy), but I don't know how to structure the packages.
Am I supposed to put both the command line entry point as well as the web UI into their own packages in the same project (which leaves the root empty). Or maybe I should move the actual generation library to the root and the UIs in separate packages. I guess the other option is to have the UIs in separate projects on github, but they are only going to be used for this library, so it does not seem like a good idea.
I remember seeing in some projects packages named cmd for example, but never have I encountered one, with multiple front ends. Is there a go(-gettable-)way for doing this?
答案1
得分: 5
我同意,如果这些项目/仓库只用于这个库,那么创建单独的项目/仓库没有太多意义。我建议只创建一个cmd
目录,并在其中为每个可执行文件创建一个子目录。
类似这样的结构:
- github.com/user/project
- generation
- cmd
- cmdline
- main.go
- web
- main.go
- cmdline
main.go
文件可以使用你拆分到"generation"包中的功能。
go build
生成的可执行文件的名称将是父目录的名称,在这个例子中是cmdline
和web
(你可以选择更好的名称)。
注意:实际上你并没有一个cmdline
或web
包。这些目录中的文件都应该属于它们自己独立的main
包。
英文:
I agree that there's not much point in making separate projects/repositories if they're only going to be used for this library. I would just have a cmd
directory with a subdirectory for each executable you're building.
Something like this:
- github.com/user/project
- generation
- cmd
- cmdline
- main.go
- web
- main.go
- cmdline
The main.go files can use the functionality that you've broken out into your "generation" package.
The name of the executables produced by go build
will be the name of the parent directory, so cmdline
and web
in this example (you would want to choose better names).
Note: you don't actually have a package cmdline
or web
. The files in those directories would all be in [their own separate] package main
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论