首页  >  文章  >  后端开发  >  使用 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