Skip to content

Commit

Permalink
Examples for new Bazel tutorial in Go (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayconrod authored Oct 29, 2024
1 parent 51926ce commit fdfabba
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions go-tutorial/stage1/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.4.0
6 changes: 6 additions & 0 deletions go-tutorial/stage1/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@rules_go//go:def.bzl", "go_binary")

go_binary(
name = "hello",
srcs = ["hello.go"],
)
4 changes: 4 additions & 0 deletions go-tutorial/stage1/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bazel_dep(
name = "rules_go",
version = "0.50.1",
)
7 changes: 7 additions & 0 deletions go-tutorial/stage1/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Hello, Bazel! 💚")
}
1 change: 1 addition & 0 deletions go-tutorial/stage2/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.4.0
7 changes: 7 additions & 0 deletions go-tutorial/stage2/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@rules_go//go:def.bzl", "go_binary")

go_binary(
name = "print_fortune",
srcs = ["print_fortune.go"],
deps = ["//fortune"],
)
4 changes: 4 additions & 0 deletions go-tutorial/stage2/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bazel_dep(
name = "rules_go",
version = "0.50.1",
)
8 changes: 8 additions & 0 deletions go-tutorial/stage2/fortune/BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
13 changes: 13 additions & 0 deletions go-tutorial/stage2/fortune/fortune.go
Original file line number Diff line number Diff line change
@@ -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))]
}
11 changes: 11 additions & 0 deletions go-tutorial/stage2/print_fortune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"

"github.com/bazelbuild/examples/go-tutorial/stage2/fortune"
)

func main() {
fmt.Println(fortune.Get())
}
1 change: 1 addition & 0 deletions go-tutorial/stage3/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.4.0
7 changes: 7 additions & 0 deletions go-tutorial/stage3/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@rules_go//go:def.bzl", "go_binary")

go_binary(
name = "print_fortune",
srcs = ["print_fortune.go"],
deps = ["//fortune"],
)
4 changes: 4 additions & 0 deletions go-tutorial/stage3/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bazel_dep(
name = "rules_go",
version = "0.50.1",
)
14 changes: 14 additions & 0 deletions go-tutorial/stage3/fortune/BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
13 changes: 13 additions & 0 deletions go-tutorial/stage3/fortune/fortune.go
Original file line number Diff line number Diff line change
@@ -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))]
}
14 changes: 14 additions & 0 deletions go-tutorial/stage3/fortune/fortune_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
11 changes: 11 additions & 0 deletions go-tutorial/stage3/print_fortune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"

"github.com/bazelbuild/examples/go-tutorial/stage3/fortune"
)

func main() {
fmt.Println(fortune.Get())
}

0 comments on commit fdfabba

Please sign in to comment.