# 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/[email protected]
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