成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何優(yōu)雅的組織Golang項目結(jié)構(gòu)

開發(fā) 前端
基于Clean Architecture原則的go-clean-template在需要將業(yè)務(wù)邏輯、數(shù)據(jù)訪問和接口層明確分離以提高可維護性和可測試性的項目中表現(xiàn)出色。

一個Go項目的結(jié)構(gòu)設(shè)計始終遵循Go語言的簡潔高效理念。一個合理和良好的布局可以提高代碼的可讀性,簡化依賴管理,并優(yōu)化編譯過程。

像cmd、internal和docs這樣的目錄是標(biāo)準(zhǔn)Go項目的基礎(chǔ),起著不同的作用。比如,cmd目錄是可執(zhí)行文件的入口,docs是所有相關(guān)文檔的入口,而internal包含項目私有代碼。適當(dāng)?shù)陌鼊澐挚梢员苊庋h(huán)依賴,便于單元測試和代碼復(fù)用。

然而,這種Go項目布局可能導(dǎo)致過多的目錄層級和包劃分,會給管理工作帶來負(fù)擔(dān),并有時讓初學(xué)者感到不知所措。

因此,在設(shè)計時如何把握什么算是“合理”,就成了關(guān)鍵。

這篇文章,讓我們嘗試在目錄結(jié)構(gòu)和包設(shè)計中找到簡潔和功能之間的平衡,使項目能夠在變化中健康迭代。

標(biāo)準(zhǔn)布局

像cmd和internal這樣的目錄結(jié)構(gòu)是由一些Go語言社區(qū)的開發(fā)者在系統(tǒng)總結(jié)之前進行了總結(jié),并在標(biāo)準(zhǔn)Go項目布局中得到了進一步的概括,該項目已經(jīng)獲得了超過4萬個star。盡管其起源是一個提示,但標(biāo)準(zhǔn)布局已經(jīng)成為Go項目目錄結(jié)構(gòu)的事實標(biāo)準(zhǔn)。

這并不是由核心Go開發(fā)團隊定義的官方標(biāo)準(zhǔn)。

my-app/ # Root directory of the project
|── cmd/ # Executables directory
└── myapp/ # Main application package
└── main.go # Main application entry point
|── internal/ # Private application and library code
└── package1/ # Internal package 1
|── pkg/ # Library code that can be exported
└── package2/ # External package 2
|── api/ # API protocol definitions directory
|── configs/ # Configuration files directory
|── deployments/ # Deployment configurations and scripts
|── scripts/ # Scripts for build, install, analysis, etc.
|── build/ # Packaging and Continuous Integration
|── tests/ # Additional external test apps and test data
|── docs/ # Design and user documents
|── tools/ # Supporting tools for the project
|── examples/ # Application or public library examples
|── third_party/ # External helper tools, forked code, and other 3rd party utilities
|── githooks/ # Git hooks
|── assets/ # Other assets like images, logos, etc.
|── vendor/ # Dependency package directory (if using vendor mode)
|── go.mod # Module dependency file
|── go.sum # Module checksum file for dependency verification

如果你經(jīng)常閱讀源代碼,你會輕易地發(fā)現(xiàn),大多數(shù)在GitHub上知名的Go開源項目基本上都遵循上述布局,比如Kubernetes這個大型Go項目。

圖片圖片

讓我們簡單看一下。

  • 與Go模塊相關(guān)的go.mod和go.sum是必不可少的。
  • pkg目錄包含api、apis、kubectl等包,可應(yīng)用于外部項目,比如基于Kubernetes的開發(fā)。
  • cmd包含了Kubernetes中各種命令行的main方法入口,比如kubectl.go。
  • api目錄存儲與openApiv3相關(guān)的json文件。
  • test目錄包含所有的e2e和集成測試代碼,根據(jù)不同的包進行了分別存儲。
  • third_party存儲第三方引用的工具,比如protobuf。
  • vendor用于存儲外部依賴,比如k8s.io、etcd等。
  • docs目錄目前為空。

當(dāng)然,Kubernetes項目并不完全遵循標(biāo)準(zhǔn)布局,因為其規(guī)模較大。例如,許多Kubernetes腳本存儲在build和cluster目錄中,而不是scripts目錄。還有一些用于特定需求的目錄,比如hacks和staging。

官方布局

2023年發(fā)布的文章《組織Go模塊》揭示了Go團隊對布局的不同觀點,提供了根據(jù)項目復(fù)雜性設(shè)計目錄結(jié)構(gòu)的參考,涵蓋了具有少量Go文件、單個cmd命令或簡單包的項目,以及具有多個cmds和多個包的項目。

對它們進行總結(jié)如下,并將其作為下一節(jié)的官方布局。

my-module/                 # Root directory for the module with go.mod
├── go.mod                 # Module's dependency file
├── go.sum                 # The module's checksums for dependency validation
├── cmd/                   # Directory for commands (each subdirectory here is a main package)
│   └── my-app/            # Main application for this module
│       └── main.go        # Main application entry point
├── internal/              # Internal packages meant for use only within this module
│   └── mylib/             # An internal package example
│       └── mylib.go       # The package's specific code
├── api/                   # API protocol definitions, e.g., protocol buffer files
├── web/                   # Web application specific components: static files, server-side templates, SPAs, etc.
├── pkg/                   # Library code that's ok to use by external applications (deprecated by some in the community)
│   └── mypkg/             # An example package that could be imported by other applications
│       └── mypkg.go       # Package code
├── README.md              # Project README file
├── LICENSE                # License file
├── .gitignore             # Specifies intentionally untracked files to ignore
└── ...                    <-- Other directories and files as needed

標(biāo)準(zhǔn)布局與官方布局

這兩種布局有一些共同的思想。

  • 模塊化。不同的功能會被放入不同的包中,以提高可重用性。
  • 提高可見性。根據(jù)功能將不同的包存儲在不同的目錄中,以提高可讀性。

基于這些概念,標(biāo)準(zhǔn)布局中有一個通用的cmd目錄來存儲命令行代碼,子包用于保存多個命令,internal目錄用于保存不對外共享的代碼。目錄路徑和包名稱與main.go作為入口文件保持一致。

但是,它們對于像pkg和util這樣的目錄有不同的考慮,例如,Russ Cox反對以pkg和util等模糊方式命名軟件庫。此外,由于社區(qū)的貢獻,標(biāo)準(zhǔn)布局比官方建議覆蓋范圍更廣,添加了像scripts和build這樣的目錄。

Go-Clean-Template

標(biāo)準(zhǔn)布局和官方布局都是通用的,將項目分為cmd項目和非cmd項目,因此對于包含entity和dao等多個包的復(fù)雜項目(如使用Go開發(fā)的Web項目),它們并不是首選。go-clean-template則專為這類Web項目量身定制。

go-clean-template/       <-- Root directory of the project
├── cmd/                 <-- Main application entry points
│   └── appname/         <-- Specific startup logic and configuration for 'appname' app
│       └── main.go      <-- Main application startup entry
├── internal/            <-- Internal modules, not importable by external applications
│   ├── entity/          <-- Entities (business objects) of the application
│   ├── usecase/         <-- Use case layer containing business logic interfaces
│   ├── repository/      <-- Data storage interfaces
│   ├── handler/         <-- HTTP handlers for receiving requests and calling use cases
│   └── config/          <-- Configuration related code
├── pkg/                 <-- Public library code that can be imported by other projects
├── test/                <-- External testing code
├── .dockerignore        <-- Specifies files to ignore in Docker builds
├── .gitignore           <-- Specifies intentionally untracked files to ignore in Git
├── Dockerfile           <-- Docker configuration file for containerization
├── go.mod               <-- Go module dependencies file
├── go.sum               <-- Checksums for Go module dependencies
└── Makefile             <-- Makefile containing automation commands

作為標(biāo)準(zhǔn)布局的擴展,go-clean-template保留了pkg目錄,以便更好地管理眾多公共庫。它明確定義了像entity、repository、config等子包,省去了我們?yōu)樗鼈兠墓ぷ鳌K€有像test和integration-test這樣的目錄,用于放置相應(yīng)的測試代碼,提高可讀性。

小結(jié)

本文帶大家深入研究了組織Go代碼庫的三種布局:

標(biāo)準(zhǔn)布局提供了一個由社區(qū)驅(qū)動的、廣泛適用的結(jié)構(gòu),非常適合需要清晰關(guān)注點分離的大型項目。

官方布局由Go的創(chuàng)建者認(rèn)可,強調(diào)簡潔和靈活性,適用于各種項目,特別是那些優(yōu)先考慮模塊管理的項目。

基于Clean Architecture原則的go-clean-template在需要將業(yè)務(wù)邏輯、數(shù)據(jù)訪問和接口層明確分離以提高可維護性和可測試性的項目中表現(xiàn)出色。

這三種范式適應(yīng)不同的項目需求,每種都提供了一套可自適應(yīng)和組合的指南。選擇其中之一取決于具體項目的要求、規(guī)模和復(fù)雜性。

責(zé)任編輯:武曉燕 來源: 程序新視界
相關(guān)推薦

2014-09-29 09:31:35

Angular

2020-11-23 14:16:42

Golang

2024-04-02 09:55:36

GolangColly開發(fā)者

2025-01-13 06:00:00

Go語言gRPC

2023-02-23 19:31:05

Golang函數(shù)重載

2021-12-07 08:16:34

React 前端 組件

2024-05-16 10:59:16

Vue項目前端

2009-03-24 13:04:55

匯總組織結(jié)構(gòu)Oracle

2015-11-26 10:53:45

LinuxWindowsMac OS

2021-01-19 10:35:49

JVM場景函數(shù)

2017-07-26 11:32:50

NETRabbitMQ系統(tǒng)集成

2013-04-10 10:40:41

2013-04-10 10:48:56

2021-01-19 05:46:00

算法javascript函數(shù)

2024-02-21 09:32:18

開發(fā)架構(gòu)

2023-10-10 13:23:18

空指針異常Java

2020-08-26 07:17:19

通信

2024-06-24 14:19:48

2022-09-09 15:17:02

CentOS 7Linux

2023-10-19 19:42:25

IstioPodkubernetes
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 免费成人高清在线视频 | 国产一区二区三区在线视频 | 99精品网 | 日韩欧美在线一区 | 伊伊综合网 | 国产一区久久 | 日韩精品一区二区三区中文在线 | 91麻豆精品国产91久久久久久 | 日本精品久久久久 | 日韩在线大片 | 日韩精品在线一区二区 | 国产成人综合在线 | 天堂久久一区 | 91色视频在线观看 | 性色视频 | heyzo在线| 在线第一页| 韩日一区二区三区 | 91亚洲视频在线 | 国产一区二区三区四区在线观看 | 天天想天天干 | 久久久久国产一区二区三区四区 | 99久久99 | 九九导航 | 午夜免费福利片 | 一二区成人影院电影网 | 欧美一区二区三区精品免费 | 成人在线免费电影 | 网页av| 亚洲日产精品 | 中文字幕 在线观看 | 国产精品国产成人国产三级 | 日韩精品一区二区三区视频播放 | 欧美一区二区三区在线 | 免费视频一区二区 | 精品久久久久久红码专区 | 国产精品久久久久久久久图文区 | avhd101在线成人播放 | 日韩视频一区在线观看 | 久久久久国产 | 精品国产一区二区在线 |