在寫了「使用Bazel、Gazelle 和bzlmod 在monorepo 中使用Go 進行構建」的文章並與一些同事分享後,我發現人們對monorepo 開發越來越感興趣。我了解到,沒有多少人仍然有足夠的經驗來了解它可以帶來的好處。所以我決定將其轉換為一個系列,從這篇關於使用 Bazel 和 Go lang 的簡單 hello world 程式的文章開始
在這篇文章中,我將嘗試介紹 Bazel 的一些極其基本的概念以及程式碼片段,以幫助人們立即上手。
巴澤爾是什麼?
依據官方文件
Bazel 是一個類似 Make、Maven 和 Gradle 的開源建置和測試工具。它使用人類可讀的高級構建語言。 Bazel 支援多種語言的專案並為多個平台建立輸出。 Bazel 支援跨多個儲存庫的大型程式碼庫和大量使用者。
它已經在 Google 使用了數十年,並且經過了徹底的實戰測試。您可以在上面鏈接的帖子中詳細了解我如何意識到這一點。
第 1 步:設定儲存庫
出於本系列的目的,我在 github.com/nixclix/basil 創建了一個存儲庫,該存儲庫將隨著時間的推移而不斷發展。在撰寫本文時,最新提交為 https://github.com/nixclix/basil/tree/d8af2aea6fb8b692f735f9959429df9fcd28422b
所以,繼續在您想要選擇的任何提供者上建立一個新的 git 儲存庫
對於 .gitignore,我強烈建議添加以下內容,以免在提交中包含不必要的文件
# If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore # bazel-* /.ijwb /.clwb /.idea /.project *.swp /.bazelrc.user # macOS-specific excludes .DS_Store # Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ # Go workspace file go.work go.work.sum # env file .env
步驟 2: 新增一些 Bazel 樣板
從 Bazel 6.3 開始,您不再需要 WORKSPACE 檔案。因此,我們將從在儲存庫的根目錄中建立以下內容開始
模組.bazel
"""Building go applications in a monorepo environment""" module(name = "basil", version = "0.0.0") http_file = use_repo_rule( "@bazel_tools//tools/build_defs/repo:http.bzl", "http_file" ) http_archive = use_repo_rule( "@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive" ) # https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/bzlmod.md bazel_dep(name = "rules_go", version = "0.50.1") bazel_dep(name = "gazelle", version = "0.39.1") GO_VERSION = "1.23.2" go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk") go_sdk.download(version = GO_VERSION) go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") go_deps.from_file(go_mod = "//:go.mod")
這裡是我們設定要使用的 go 版本以及瞪羚和rules_go 版本的地方。
我們將使用 Gazelle 進行 BUILD 檔案管理。 Gazelle 讓產生建置檔案變得非常方便。您可以在這裡閱讀更多相關資訊
BUILD.bazel
load("@gazelle//:def.bzl", "gazelle") gazelle(name = "gazelle") gazelle( name = "gazelle-update-repos", args = [ "-from_file=go.mod", "-to_macro=deps.bzl%go_dependencies", "-prune", ], command = "update-repos", )
這應該位於儲存庫的根目錄。這會指示瞪羚了解 go mod 檔案的來源以及其他要運行的進程
接下來我們再建立 3 個包含各自內容的檔案。現在不用擔心這些的作用。
.bazelignore
runfiles/ bzlmod/
.bazelrc
# Enable Bzlmod for every Bazel command common --enable_bzlmod
.bazel版本
# If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore # bazel-* /.ijwb /.clwb /.idea /.project *.swp /.bazelrc.user # macOS-specific excludes .DS_Store # Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ # Go workspace file go.work go.work.sum # env file .env
好的,至此您應該具備了進行一些基礎工作所需的一切。現在,如果您在根目錄中運行 bazel build //... ,bazel 應該能夠遍歷儲存庫並建立它發現的任何套件。 Bazel 快取早期建置的套件輸出,因此從這裡開始的任何後續建置都應該非常快。
接下來,讓我們開始破解實際的 hello world 程式。
第三步:寫helloworld包
對於程式碼的基本組織,我們將在名為 /packages 的目錄中編寫所有 Go 程式碼。這樣,程式碼其他部分的任何引用都可以將其引用為 //packages/...
讓我們在packages目錄中建立一個名為helloworld的目錄並加入以下內容
helloworld.go
"""Building go applications in a monorepo environment""" module(name = "basil", version = "0.0.0") http_file = use_repo_rule( "@bazel_tools//tools/build_defs/repo:http.bzl", "http_file" ) http_archive = use_repo_rule( "@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive" ) # https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/bzlmod.md bazel_dep(name = "rules_go", version = "0.50.1") bazel_dep(name = "gazelle", version = "0.39.1") GO_VERSION = "1.23.2" go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk") go_sdk.download(version = GO_VERSION) go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") go_deps.from_file(go_mod = "//:go.mod")
BUILD.bazel
load("@gazelle//:def.bzl", "gazelle") gazelle(name = "gazelle") gazelle( name = "gazelle-update-repos", args = [ "-from_file=go.mod", "-to_macro=deps.bzl%go_dependencies", "-prune", ], command = "update-repos", )
go 程式相當簡單。我們有一個簡單的 main 函數,可以簡單地列印一條 hello world 訊息。
我們感興趣的是 BUILD.bazel 檔案。讓我們逐塊地看一下這個並嘗試理解它的含義。
第一行從rules_go載入go_binary和go_library巨集。您會發現這些在稍後的程式碼中被使用。
在第 10 行中,我們定義了一個名為 helloworld_lib 的函式庫,並將該函式庫的來源指定為 helloworld.go。如果這個套件需要我導入,那麼您也可以指定它的可用路徑,即 basil/packages/helloworld_lib。然後是可見性,在這裡我們指定該庫是私有的,僅在此套件中可見。在以後的文章中,我們可能會研究如何更改這些參數以使用其他套件的依賴項。
在第 3 行中,我們使用 Rules_go 中的 go_binary 巨集為我們的程式產生二進位檔案。這裡我們指定先前在 embed 參數中定義的函式庫。包可見性也適用於二進位檔案。
第 4 步:建置並運行
就是這樣。瞧!您可以透過先使用 bazel build //packages/helloworld 然後使用 bazel run //packages/helloworld
建立二進位檔案來執行該二進位檔案如果您喜歡這篇文章,並且希望獲得以此為基礎的未來帖子作為該系列的一部分,請訂閱並分享這篇文章。
以上是使用 Bazel 和 Go lang 的簡單 hello world 程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文解釋了GO的軟件包導入機制:命名imports(例如導入“ fmt”)和空白導入(例如導入_ fmt; fmt;)。 命名導入使包裝內容可訪問,而空白導入僅執行t

本文詳細介紹了MySQL查詢結果的有效轉換為GO結構切片。 它強調使用數據庫/SQL的掃描方法來最佳性能,避免手動解析。 使用DB標籤和Robus的結構現場映射的最佳實踐

本文解釋了Beego的NewFlash()函數,用於Web應用程序中的頁間數據傳輸。 它專注於使用newflash()在控制器之間顯示臨時消息(成功,錯誤,警告),並利用會話機制。 Lima

本文探討了GO的仿製藥自定義類型約束。 它詳細介紹了界面如何定義通用功能的最低類型要求,從而改善了類型的安全性和代碼可重複使用性。 本文還討論了局限性和最佳實踐

本文演示了創建模擬和存根進行單元測試。 它強調使用接口,提供模擬實現的示例,並討論最佳實踐,例如保持模擬集中並使用斷言庫。 文章

本文詳細介紹了在GO中詳細介紹有效的文件,將OS.WriteFile(適用於小文件)與OS.openfile和緩衝寫入(最佳大型文件)進行比較。 它強調了使用延遲並檢查特定錯誤的可靠錯誤處理。

本文使用跟踪工具探討了GO應用程序執行流。 它討論了手冊和自動儀器技術,比較諸如Jaeger,Zipkin和Opentelemetry之類的工具,並突出顯示有效的數據可視化


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境