Monorepo Golang application With Bazel

Buyung Hardiansyah
2 min readJan 2, 2020

--

ini this section, you’ll learn the basic of building go application with bazel(https://www.bazel.build/) and gazelle (https://github.com/bazelbuild/bazel-gazelle)

first install bazel, if you don’t have it, install bazel here :(https://docs.bazel.build/versions/2.0.0/install.html)

lets create hello-world project with go language.

create folder packages, WORKSPACE file, & BUILD.bazel file in root project.

bazel-go
└── packages
├──hello-world
│ ├── main.go
│ ├── BUILD.bazel
│ ├── ext
│ │ ├── counting.go
├──WORKSPACE
├──BUILD.bazel

packages folder is a folder to store multiple project.

create folder hello-world in packages folder (packages/hello-world).

now, lets create a simple hello-world and its dependency.
create file main.go in folder packages/hello-world :
main.go

package mainimport (
"fmt"
"github.com/hardyantz/go-hello-world/ext"
)
func main() {
fmt.Println("hello world")
count := ext.Counting(4, 2)
fmt.Println(count)
}

create sample dependency in file ext/counting.go:

package extfunc Counting (a int, b int) int {
return a + b
}

create BUILD.bazel file in packages/hello-world/ this tells bazel to locate the project we want to build :

load("@bazel_gazelle//:def.bzl", "gazelle")# gazelle:prefix github.com/hardyantz/go-hello-worldgazelle(name = "gazelle")load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")go_binary(
name = "hello-world",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

update WORKSPACE file in root project :


workspace(name = "go_monorepo")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")http_archive(
name = "io_bazel_rules_go",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/v0.20.1/rules_go-v0.20.1.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/v0.20.1/rules_go-v0.20.1.tar.gz",
],sha256="842ec0e6b4fbfdd3de6150b61af92901eeb73681fd4d185746644c338f51d4c0",)http_archive(
name = "bazel_gazelle",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/bazel-gazelle/releases/download/0.18.2/bazel-gazelle-0.18.2.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/0.18.2/bazel-gazelle-0.18.2.tar.gz",
],
sha256 = "7fc87f4170011201b1690326e8c16c5d802836e3a0d617d8f75c3af2b23180c4",)load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")go_rules_dependencies()go_register_toolchains()load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")gazelle_dependencies()

update BUILD.bazel file :

load("@bazel_gazelle//:def.bzl", "gazelle")# gazelle : exclude build# gazelle:prefix github.com/hardyantz/gazelle-bazelgazelle(name = "gazelle")

now run the following code to generate new BUILD.bazel files for your project.

$ bazel run //:gazelle

you can run the same command in the future to update existing BUILD.bazel files to include new source files or options.

now lets build hello-world go application :

$ bazel run //packages/hello-world:hello-world

if you want to build application into the binary file run this command :

$ bazel build //packages/hello-world:hello-world

bazel will show you where the binary target file is.
in this example and build in mac os X, binary file located in :

bazel-bin/packages/hello-world/darwin_amd64_stripped/hello-world

run binary file :

$ ./bazel-bin/packages/hello-world/darwin_amd64_stripped/hello-world

Share with your friends. Clap 👏 as max as 50 times

--

--

Buyung Hardiansyah
Buyung Hardiansyah

Written by Buyung Hardiansyah

Software Developer -- Golang | Python | PHP | Reactjs | Flutter

Responses (2)