README
ΒΆ
Golang
Golang Interactive Playground
Bazel
is designed to work at scale and supports incremental hermetic builds across a distributed infrastructure, which is necessary for large codebase. With Bazel Go ruleset, you are able to manage the Go toolchain and external libraries without depending on locally installed ones.
Gazelle
is used to generate Go and Protocol Buffers rules. With Gazelle
, you are able to generate Bazel
rules for most Go packages in our Go monorepo with minimal human input. Gazelle
can also import the versions of Go modules into Bazel rules so we can conveniently and efficiently build external libraries.
π git clone https://github.com/terrencemiao/golang src
π go mod init github.com/terrencemiao/golang
A file go.mod
is created:
π cat go.mod
module github.com/terrencemiao/golang
go 1.16
Now run the command:
π bazel run //:gazelle
which tells bazel
to run the gazelle
target specified in the BUILD
file. This will autogenerate the BUILD.bazel
files for all of the packages.
π tree -C
.
βββ BUILD
βββ LICENSE
βββ README.md
βββ WORKSPACE
βββ bazel
βΒ Β βββ docker
βΒ Β βΒ Β βββ BUILD
βΒ Β βΒ Β βββ def.bzl
βΒ Β βΒ Β βββ repos.bzl
βΒ Β βββ go
βΒ Β βββ BUILD
βΒ Β βββ WORKSPACE
βΒ Β βββ def.bzl
βΒ Β βββ repos.bzl
βββ bazel-bin -> /private/var/tmp/_bazel_terrence/3...6/execroot/__main__/bazel-out/darwin-fastbuild/bin
βββ bazel-out -> /private/var/tmp/_bazel_terrence/3...6/execroot/__main__/bazel-out
βββ bazel-src -> /private/var/tmp/_bazel_terrence/3...6/execroot/__main__
βββ bazel-testlogs -> /private/var/tmp/_bazel_terrence/3...6/execroot/__main__/bazel-out/darwin-fastbuild/testlogs
βββ go.mod
βββ go_third_party.bzl
βββ link_go.sh
βββ protos
βΒ Β βββ common
βΒ Β βΒ Β βββ BUILD.bazel
βΒ Β βΒ Β βββ common.proto
βΒ Β βββ hello
βΒ Β βββ BUILD.bazel
βΒ Β βββ hello.proto
βΒ Β βββ hello_service.proto
βββ services
βββ hello
βββ BUILD.bazel
βββ main.go
βββ server
βββ BUILD.bazel
βββ server.go
βββ server_test.go
13 directories, 24 files
In addition, *.pb.go
artefact files also generated:
π find bazel-out/ -name "*.pb.go"
bazel-out//darwin-fastbuild/bin/protos/common/common_go_proto_/github.com/terrencemiao/golang/protos/common/common.pb.go
bazel-out//darwin-fastbuild/bin/protos/hello/hello_go_proto_/github.com/terrencemiao/golang/protos/hello/hello_service.pb.go
bazel-out//darwin-fastbuild/bin/protos/hello/hello_go_proto_/github.com/terrencemiao/golang/protos/hello/hello.pb.go
Now, inform bazel
about the dependencies mentioned in go.mod
file. Either:
π go get github.com/bazelbuild/bazel-gazelle/cmd/gazelle
π gazelle -go_prefix github.com/terrencemiao/golang
π gazelle update-repos --from_file=go.mod -to_macro=go_third_party.bzl%go_deps
or, with bazel
:
π bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=go_third_party.bzl%go_deps
Compile hello service:
π bazel build //services/hello
INFO: Analyzed target //services/hello:hello (117 packages loaded, 1553 targets configured).
INFO: Found 1 target...
Target //services/hello:hello up-to-date:
bazel-bin/services/hello/hello_/hello
INFO: Elapsed time: 2.331s, Critical Path: 0.06s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
Run hello service, with default proxy port 24689:
π bazel run //services/hello
INFO: Analyzed target //services/hello:hello (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //services/hello:hello up-to-date:
bazel-bin/services/hello/hello_/hello
INFO: Elapsed time: 0.430s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
2021/07/17 20:04:25 Setting proxy server port 24689
Run hello service, with proxy port 8082:
π bazel run //services/hello -- --proxy-port 8082
INFO: Analyzed target //services/hello:hello (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //services/hello:hello up-to-date:
bazel-bin/services/hello/hello_/hello
INFO: Elapsed time: 0.538s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
2021/07/17 20:06:03 Setting proxy server port 8082
Testing
Error thrown when run:
π bazel test //...
...
ERROR: golang/src/services/hello/server/BUILD.bazel:14:8: no such package '@com_github_stretchr_testify//require': The repository '@com_github_stretchr_testify' could not be resolved and referenced by '//services/hello/server:server_test'
...
Solution fix this issue:
π bazel run //:gazelle -- update-repos github.com/stretchr/testify
Publishing a module
Removes any dependencies the module might have accumulated that are no longer necessary.
π go mod tidy
go mod tidy
warning: ignoring symlink /Users/terrence/Projects/golang/src/bazel-bin
warning: ignoring symlink /Users/terrence/Projects/golang/src/bazel-out
warning: ignoring symlink /Users/terrence/Projects/golang/src/bazel-src
warning: ignoring symlink /Users/terrence/Projects/golang/src/bazel-testlogs
Tag the project with a version number.
π git tag -a v1.2.6 -m "Publish module version v1.2.6"
π git push origin v1.2.6
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 829 bytes | 829.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/terrencemiao/golang.git
* [new tag] v1.2.6 -> v1.2.6
Publish Golang module.
Golang packages are given lower case, single-word names; there should be no need for underscores or mixedCaps.
π env GOPROXY=proxy.golang.org go list -m github.com/terrencemiao/golang@v1.2.6
github.com/terrencemiao/golang v1.2.6
Can find the published Golang module at https://pkg.go.dev/github.com/terrencemiao/golang
Reference
- How to Golang Monorepo, https://medium.com/goc0de/how-to-golang-monorepo-4f62320a01fd
- Go rules for Bazel, https://github.com/bazelbuild/rules_go