首頁  >  文章  >  後端開發  >  使用 Bazel 和 Go lang 的簡單 hello world 程序

使用 Bazel 和 Go lang 的簡單 hello world 程序

Susan Sarandon
Susan Sarandon原創
2024-10-25 07:39:02880瀏覽

Simple hello world program using Bazel and Go lang

在寫了「使用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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn