diff --git a/go-tutorial/stage1/.bazelversion b/go-tutorial/stage1/.bazelversion new file mode 100644 index 000000000..ba7f754d0 --- /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 000000000..e5ff83314 --- /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 000000000..2696185de --- /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 000000000..06e83e7b2 --- /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 000000000..ba7f754d0 --- /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 000000000..1c2c3b180 --- /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 000000000..2696185de --- /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 000000000..48bcb3d35 --- /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 000000000..6ad8dd8f6 --- /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 000000000..fb1585756 --- /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 000000000..ba7f754d0 --- /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 000000000..1c2c3b180 --- /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 000000000..2696185de --- /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 000000000..246af4556 --- /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 000000000..6ad8dd8f6 --- /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 000000000..4ca66993b --- /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 000000000..b01ced657 --- /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()) +}