Skip to content

Instantly share code, notes, and snippets.

@craftslab
Last active January 29, 2024 08:41
Show Gist options
  • Save craftslab/6050b2a67adc26bd365ea8c05f24e7b1 to your computer and use it in GitHub Desktop.
Save craftslab/6050b2a67adc26bd365ea8c05f24e7b1 to your computer and use it in GitHub Desktop.
bazel go

Install

sudo apt install apt-transport-https curl gnupg -y
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor >bazel-archive-keyring.gpg
sudo mv bazel-archive-keyring.gpg /usr/share/keyrings
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/bazel-archive-keyring.gpg] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
sudo apt update && sudo apt install -y bazel-6.4.0

Init

go mod init github.com/craftslab/hello
bazel run //:gazelle

Build

# target: bazel-bin/hello_/hello
bazel run //:hello

# target: bazel-bin/hello_go_proto_/github.com/craftslab/hello/hello.pb.go
bazel build //:hello_go_proto

Reference

load("@bazel_gazelle//:def.bzl", "gazelle")
# gazelle:prefix github.com/craftslab/hello
gazelle(name = "gazelle")
package main
import (
"os"
)
func main() {
println("hello")
os.Exit(0)
}
syntax = "proto3";
option go_package = "github.com/craftslab/hello";
package hello;
// The service definition.
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message.
message HelloRequest {
string name = 1;
}
// The response message.
message HelloReply {
string message = 1;
}
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
sha256 = "d6b2513456fe2229811da7eb67a444be7785f5323c6708b38d851d2b51e54d83",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.30.0/rules_go-v0.30.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.30.0/rules_go-v0.30.0.zip",
],
)
http_archive(
name = "bazel_gazelle",
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
],
)
http_archive(
name = "com_google_protobuf",
sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113",
strip_prefix = "protobuf-3.14.0",
urls = [
"https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
"https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
],
)
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
go_rules_dependencies()
go_register_toolchains(version = "1.17.6")
gazelle_dependencies()
protobuf_deps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment