Categorygithub.com/terrencemiao/golang
module
1.2.6
Repository: https://github.com/terrencemiao/golang.git
Documentation: pkg.go.dev

# 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

# Packages

No description provided by the author
No description provided by the author