README ΒΆ
Golang
Golang Interactive Playground
Let' get start.
π 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
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
Publishing a module
Removes any dependencies the module might have accumulated that are no longer necessary.
π 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
go: finding module for package github.com/TerrenceMiao/golang/protos/hello
go: finding module for package github.com/jessevdk/go-flags
go: finding module for package github.com/TerrenceMiao/golang/protos/common
go: finding module for package google.golang.org/grpc
go: finding module for package github.com/stretchr/testify/require
go: found github.com/jessevdk/go-flags in github.com/jessevdk/go-flags v1.5.0
go: found google.golang.org/grpc in google.golang.org/grpc v1.39.0
go: found github.com/stretchr/testify/require in github.com/stretchr/testify v1.7.0
go: downloading golang.org/x/net v0.0.0-20200822124328-c89045814202
go: downloading github.com/golang/protobuf v1.4.3
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: downloading google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
go: downloading github.com/google/go-cmp v0.5.0
go: downloading google.golang.org/protobuf v1.25.0
Tag the project with a version number.
π git tag -a 1.0.0 -m "Publish module version 1.0.0"
π git push origin 1.0.0
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] 1.0.0 -> 1.0.0
Publish Golang module.
π env GOPROXY=proxy.golang.org go list -m github.com/TerrenceMiao/golang@1.0.0
github.com/TerrenceMiao/golang v0.0.0-20210717101020-6b3b278957f1
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
Click to show internal directories.
Click to hide internal directories.