总体要求

  • 命令清晰:目录名要清晰简洁,不要太长,也不要太短,最好用单数(单数足以说明功能,都用单数可避免单复混用的情况)
  • 功能明确:一个目录实现的功能应该是明确的,并且在项目目录中具有很高的辨识度
  • 全面性:目录结构应尽可能全面地包含研发过程中需要的功能,例如文档、脚本、源码管理、API实现、工具、第三方包、测试、编译产物等
  • 可预测性:目录结构应该能够在项目变大时,仍然保持之前的目录结构
  • 可拓展性:每个目录下存放了同类的功能,在项目变大时,这些目录应该可以存放更多同类功能

下文将推荐几个Go项目和Python项目目录结构,目录结构只需符合规范即可,推荐的目录结构并非标准,仅供参考,实际项目中可因地制宜,按需修改。

Go项目目录结构

平铺式目录结构

当项目时代码框架/库时,最好使用平铺式目录结构,
平铺式目录结构的特点:在项目根目录下存放项目代码,整个目录结构看起来更像一层
结构较简单,不赘述,可直接参考下列项目:
https://github.com/bits-and-blooms/bitset

结构化目录结构

Go社区推荐的目录结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
├─api
├─build
│ ├─ci
│ └─package
├─cmd
│ └─_your_app_
├─configs
├─deployments
├─docs
├─examples
├─githooks
├─init
├─internal
│ ├─app
│ │ └─_your_app_
│ └─pkg
│ └─_your_private_lib_
├─pkg
│ └─_your_public_lib_
├─scripts
├─test
├─third_party
├─tools
├─vendor
├─web
│ ├─app
│ ├─static
│ └─template
├─website
│ .editorconfig
│ .gitignore
│ go.mod
│ LICENSE.md
│ Makefile
└── README.md

每个目录具体作用在项目中做了详细说明,此处不赘述,请参考:GitHub - golang-standards/project-layout: Standard Go Project Layout
建议:
一般小型项目用不到上述例子中那么多的目录,可以考虑先包含3个目录:cmd、pkg、internal,其他目录后面按需创建

Python项目目录结构

命令行应用程序目录结构

一次性脚本项目

有时候我们要实现的功能只需要写几个脚本,然后运行,不需要打包成包,安装……我们可以把这一类项目归类为一次性脚本项目。一次性脚本项目目录结构可以采用平铺式目录结构,简单直接,所有文件在同一个目录。例:

1
2
3
4
5
6
7
8
9
helloworld/

├── .gitignore
├── helloworld.py
├── LICENSE
├── README.md
├── requirements.txt
├── setup.py
└── tests.py

可安装包项目

有时候我们需要将脚本打包成包供第三方使用,同时想保留其他辅助项目管理的文件,例如 README, .gitignore等,这时候可以参考下面的目录结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
helloworld/

├── helloworld/
│ ├── __init__.py
│ ├── helloworld.py
│ └── helpers.py

├── tests/
│ ├── helloworld_tests.py
│ └── helpers_tests.py

├── .gitignore
├── LICENSE
├── README.md
├── requirements.txt
└── setup.py

具有内部包的应用程序

在较大的应用程序中,你可能有一个或多个内部包,这些包要么与主运行程序脚本绑定在一起,要么为正在打包的较大库提供特定功能。这时候可以参考下面的目录结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
helloworld/

├── bin/

├── docs/
│ ├── hello.md
│ └── world.md

├── helloworld/
│ ├── __init__.py
│ ├── runner.py
│ ├── hello/
│ │ ├── __init__.py
│ │ ├── hello.py
│ │ └── helpers.py
│ │
│ └── world/
│ ├── __init__.py
│ ├── helpers.py
│ └── world.py

├── data/
│ ├── input.csv
│ └── output.xlsx

├── tests/
│ ├── hello
│ │ ├── helpers_tests.py
│ │ └── hello_tests.py
│ │
│ └── world/
│ ├── helpers_tests.py
│ └── world_tests.py

├── .gitignore
├── LICENSE
└── README.md

如果想知道每个目录的具体作用,可参考:Python Application Layouts: A Reference – Real Python
作者 Kyle Stratis 做了详尽描述,再此不赘述。

Web应用程序目录结构

flask项目目录结构

官方文档中的示例目录结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/home/user/Projects/flask-tutorial
├── flaskr/
│ ├── __init__.py
│ ├── db.py
│ ├── schema.sql
│ ├── auth.py
│ ├── blog.py
│ ├── templates/
│ │ ├── base.html
│ │ ├── auth/
│ │ │ ├── login.html
│ │ │ └── register.html
│ │ └── blog/
│ │ ├── create.html
│ │ ├── index.html
│ │ └── update.html
│ └── static/
│ └── style.css
├── tests/
│ ├── conftest.py
│ ├── data.sql
│ ├── test_factory.py
│ ├── test_db.py
│ ├── test_auth.py
│ └── test_blog.py
├── venv/
├── setup.py
└── MANIFEST.in

但我觉得上述结构不好,我常用的目录结构如下,供大家参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
├─api
│ ├─api_v_1
├─configs
├─dao
├─data
├─database
│ ├─models
├─deployments
├─docs
├─scripts
├── tests
├─utils
├── cli.py
├── settings.toml
├── .dockerignore
├── .gitignore
├── LICENSE
├── requirements.txt
├── setup.py
└── README.md

参考链接:
GitHub - golang-standards/project-layout: Standard Go Project Layout
Go工程化 - Project Layout 最佳实践
golang搭配 makefile 真香!
https://github.com/bits-and-blooms/bitset
Python Application Layouts: A Reference – Real Python