From fdfabba6aa8065f5b1349931dc08678fdccdb678 Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Tue, 29 Oct 2024 03:30:04 -0700 Subject: [PATCH] Examples for new Bazel tutorial in Go (#519) --- go-tutorial/stage1/.bazelversion | 1 + go-tutorial/stage1/BUILD | 6 ++++++ go-tutorial/stage1/MODULE.bazel | 4 ++++ go-tutorial/stage1/hello.go | 7 +++++++ go-tutorial/stage2/.bazelversion | 1 + go-tutorial/stage2/BUILD | 7 +++++++ go-tutorial/stage2/MODULE.bazel | 4 ++++ go-tutorial/stage2/fortune/BUILD | 8 ++++++++ go-tutorial/stage2/fortune/fortune.go | 13 +++++++++++++ go-tutorial/stage2/print_fortune.go | 11 +++++++++++ go-tutorial/stage3/.bazelversion | 1 + go-tutorial/stage3/BUILD | 7 +++++++ go-tutorial/stage3/MODULE.bazel | 4 ++++ go-tutorial/stage3/fortune/BUILD | 14 ++++++++++++++ go-tutorial/stage3/fortune/fortune.go | 13 +++++++++++++ go-tutorial/stage3/fortune/fortune_test.go | 14 ++++++++++++++ go-tutorial/stage3/print_fortune.go | 11 +++++++++++ 17 files changed, 126 insertions(+) create mode 100644 go-tutorial/stage1/.bazelversion create mode 100644 go-tutorial/stage1/BUILD create mode 100644 go-tutorial/stage1/MODULE.bazel create mode 100644 go-tutorial/stage1/hello.go create mode 100644 go-tutorial/stage2/.bazelversion create mode 100644 go-tutorial/stage2/BUILD create mode 100644 go-tutorial/stage2/MODULE.bazel create mode 100644 go-tutorial/stage2/fortune/BUILD create mode 100644 go-tutorial/stage2/fortune/fortune.go create mode 100644 go-tutorial/stage2/print_fortune.go create mode 100644 go-tutorial/stage3/.bazelversion create mode 100644 go-tutorial/stage3/BUILD create mode 100644 go-tutorial/stage3/MODULE.bazel create mode 100644 go-tutorial/stage3/fortune/BUILD create mode 100644 go-tutorial/stage3/fortune/fortune.go create mode 100644 go-tutorial/stage3/fortune/fortune_test.go create mode 100644 go-tutorial/stage3/print_fortune.go diff --git a/go-tutorial/stage1/.bazelversion b/go-tutorial/stage1/.bazelversion new file mode 100644 index 00000000..ba7f754d --- /dev/null +++ b/go-tutorial/stage1/.bazelversion @@ -0,0 +1 @@ +7.4.0 diff --git a/go-tutorial/stage1/BUILD b/go-tutorial/stage1/BUILD new file mode 100644 index 00000000..e5ff8331 --- /dev/null +++ b/go-tutorial/stage1/BUILD @@ -0,0 +1,6 @@ +load("@rules_go//go:def.bzl", "go_binary") + +go_binary( + name = "hello", + srcs = ["hello.go"], +) diff --git a/go-tutorial/stage1/MODULE.bazel b/go-tutorial/stage1/MODULE.bazel new file mode 100644 index 00000000..2696185d --- /dev/null +++ b/go-tutorial/stage1/MODULE.bazel @@ -0,0 +1,4 @@ +bazel_dep( + name = "rules_go", + version = "0.50.1", +) diff --git a/go-tutorial/stage1/hello.go b/go-tutorial/stage1/hello.go new file mode 100644 index 00000000..06e83e7b --- /dev/null +++ b/go-tutorial/stage1/hello.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello, Bazel! 💚") +} diff --git a/go-tutorial/stage2/.bazelversion b/go-tutorial/stage2/.bazelversion new file mode 100644 index 00000000..ba7f754d --- /dev/null +++ b/go-tutorial/stage2/.bazelversion @@ -0,0 +1 @@ +7.4.0 diff --git a/go-tutorial/stage2/BUILD b/go-tutorial/stage2/BUILD new file mode 100644 index 00000000..1c2c3b18 --- /dev/null +++ b/go-tutorial/stage2/BUILD @@ -0,0 +1,7 @@ +load("@rules_go//go:def.bzl", "go_binary") + +go_binary( + name = "print_fortune", + srcs = ["print_fortune.go"], + deps = ["//fortune"], +) diff --git a/go-tutorial/stage2/MODULE.bazel b/go-tutorial/stage2/MODULE.bazel new file mode 100644 index 00000000..2696185d --- /dev/null +++ b/go-tutorial/stage2/MODULE.bazel @@ -0,0 +1,4 @@ +bazel_dep( + name = "rules_go", + version = "0.50.1", +) diff --git a/go-tutorial/stage2/fortune/BUILD b/go-tutorial/stage2/fortune/BUILD new file mode 100644 index 00000000..48bcb3d3 --- /dev/null +++ b/go-tutorial/stage2/fortune/BUILD @@ -0,0 +1,8 @@ +load("@rules_go//go:def.bzl", "go_library") + +go_library( + name = "fortune", + srcs = ["fortune.go"], + importpath = "github.com/bazelbuild/examples/go-tutorial/stage2/fortune", + visibility = ["//visibility:public"], +) diff --git a/go-tutorial/stage2/fortune/fortune.go b/go-tutorial/stage2/fortune/fortune.go new file mode 100644 index 00000000..6ad8dd8f --- /dev/null +++ b/go-tutorial/stage2/fortune/fortune.go @@ -0,0 +1,13 @@ +package fortune + +import "math/rand" + +var fortunes = []string{ + "Your build will complete quickly.", + "Your dependencies will be free of bugs.", + "Your tests will pass.", +} + +func Get() string { + return fortunes[rand.Intn(len(fortunes))] +} diff --git a/go-tutorial/stage2/print_fortune.go b/go-tutorial/stage2/print_fortune.go new file mode 100644 index 00000000..fb158575 --- /dev/null +++ b/go-tutorial/stage2/print_fortune.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + + "github.com/bazelbuild/examples/go-tutorial/stage2/fortune" +) + +func main() { + fmt.Println(fortune.Get()) +} diff --git a/go-tutorial/stage3/.bazelversion b/go-tutorial/stage3/.bazelversion new file mode 100644 index 00000000..ba7f754d --- /dev/null +++ b/go-tutorial/stage3/.bazelversion @@ -0,0 +1 @@ +7.4.0 diff --git a/go-tutorial/stage3/BUILD b/go-tutorial/stage3/BUILD new file mode 100644 index 00000000..1c2c3b18 --- /dev/null +++ b/go-tutorial/stage3/BUILD @@ -0,0 +1,7 @@ +load("@rules_go//go:def.bzl", "go_binary") + +go_binary( + name = "print_fortune", + srcs = ["print_fortune.go"], + deps = ["//fortune"], +) diff --git a/go-tutorial/stage3/MODULE.bazel b/go-tutorial/stage3/MODULE.bazel new file mode 100644 index 00000000..2696185d --- /dev/null +++ b/go-tutorial/stage3/MODULE.bazel @@ -0,0 +1,4 @@ +bazel_dep( + name = "rules_go", + version = "0.50.1", +) diff --git a/go-tutorial/stage3/fortune/BUILD b/go-tutorial/stage3/fortune/BUILD new file mode 100644 index 00000000..246af455 --- /dev/null +++ b/go-tutorial/stage3/fortune/BUILD @@ -0,0 +1,14 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "fortune", + srcs = ["fortune.go"], + importpath = "github.com/bazelbuild/examples/go-tutorial/stage3/fortune", + visibility = ["//visibility:public"], +) + +go_test( + name = "fortune_test", + srcs = ["fortune_test.go"], + embed = [":fortune"], +) diff --git a/go-tutorial/stage3/fortune/fortune.go b/go-tutorial/stage3/fortune/fortune.go new file mode 100644 index 00000000..6ad8dd8f --- /dev/null +++ b/go-tutorial/stage3/fortune/fortune.go @@ -0,0 +1,13 @@ +package fortune + +import "math/rand" + +var fortunes = []string{ + "Your build will complete quickly.", + "Your dependencies will be free of bugs.", + "Your tests will pass.", +} + +func Get() string { + return fortunes[rand.Intn(len(fortunes))] +} diff --git a/go-tutorial/stage3/fortune/fortune_test.go b/go-tutorial/stage3/fortune/fortune_test.go new file mode 100644 index 00000000..4ca66993 --- /dev/null +++ b/go-tutorial/stage3/fortune/fortune_test.go @@ -0,0 +1,14 @@ +package fortune + +import ( + "slices" + "testing" +) + +// TestGet checks that Get returns one of the strings from fortunes. +func TestGet(t *testing.T) { + msg := Get() + if i := slices.Index(fortunes, msg); i < 0 { + t.Errorf("Get returned %q, not one the expected messages", msg) + } +} diff --git a/go-tutorial/stage3/print_fortune.go b/go-tutorial/stage3/print_fortune.go new file mode 100644 index 00000000..b01ced65 --- /dev/null +++ b/go-tutorial/stage3/print_fortune.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + + "github.com/bazelbuild/examples/go-tutorial/stage3/fortune" +) + +func main() { + fmt.Println(fortune.Get()) +}