Add go flatbuffer rules

Change-Id: Ia80716772d09ccef6c4043b6e8ccb9321ed2f1e7
Signed-off-by: Alex Perry <alex.perry96@gmail.com>
diff --git a/BUILD b/BUILD
index 91997b9..e7fa267 100644
--- a/BUILD
+++ b/BUILD
@@ -11,6 +11,7 @@
 # gazelle:go_generate_proto false
 # gazelle:exclude third_party
 # gazelle:exclude external
+# gazelle:resolve go github.com/frc971/971-Robot-Code/build_tests/fbs //build_tests:test_go_fbs
 
 gazelle(
     name = "gazelle",
diff --git a/build_tests/BUILD b/build_tests/BUILD
index 6a81509..8dd0637 100644
--- a/build_tests/BUILD
+++ b/build_tests/BUILD
@@ -1,5 +1,5 @@
 load("@com_google_protobuf//:protobuf.bzl", "cc_proto_library")
-load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_py_library")
+load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_go_library", "flatbuffer_py_library")
 load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
 load("//tools/build_rules:apache.bzl", "apache_wrapper")
 load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
@@ -77,6 +77,14 @@
     visibility = ["//visibility:public"],
 )
 
+flatbuffer_go_library(
+    name = "test_go_fbs",
+    srcs = ["test.fbs"],
+    importpath = "github.com/frc971/971-Robot-Code/build_tests/fbs",
+    target_compatible_with = ["@platforms//cpu:x86_64"],
+    visibility = ["//visibility:public"],
+)
+
 py_test(
     name = "python_fbs",
     srcs = ["python_fbs.py"],
diff --git a/build_tests/go_flatbuffer/BUILD b/build_tests/go_flatbuffer/BUILD
new file mode 100644
index 0000000..d472a6f
--- /dev/null
+++ b/build_tests/go_flatbuffer/BUILD
@@ -0,0 +1,20 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
+
+go_library(
+    name = "go_flatbuffer_lib",
+    srcs = ["go_fbs.go"],
+    importpath = "github.com/frc971/971-Robot-Code/build_tests/go_flatbuffer",
+    target_compatible_with = ["@platforms//cpu:x86_64"],
+    visibility = ["//visibility:private"],
+    deps = [
+        "//build_tests:test_go_fbs",
+        "@com_github_google_flatbuffers//go:go_default_library",
+    ],
+)
+
+go_binary(
+    name = "go_flatbuffer",
+    embed = [":go_flatbuffer_lib"],
+    target_compatible_with = ["@platforms//cpu:x86_64"],
+    visibility = ["//visibility:public"],
+)
diff --git a/build_tests/go_flatbuffer/go_fbs.go b/build_tests/go_flatbuffer/go_fbs.go
new file mode 100644
index 0000000..2e936c1
--- /dev/null
+++ b/build_tests/go_flatbuffer/go_fbs.go
@@ -0,0 +1,13 @@
+package main
+
+import (
+	build_tests "github.com/frc971/971-Robot-Code/build_tests/fbs"
+	flatbuffers "github.com/google/flatbuffers/go"
+)
+
+func main() {
+	builder := flatbuffers.NewBuilder(1024)
+	build_tests.FooStart(builder)
+	build_tests.FooAddValue(builder, 3)
+	build_tests.FooEnd(builder)
+}
diff --git a/third_party/flatbuffers/build_defs.bzl b/third_party/flatbuffers/build_defs.bzl
index 5add25f..c547a7d 100644
--- a/third_party/flatbuffers/build_defs.bzl
+++ b/third_party/flatbuffers/build_defs.bzl
@@ -6,6 +6,7 @@
 """
 
 load("@npm//@bazel/typescript:index.bzl", "ts_library")
+load("@io_bazel_rules_go//go:def.bzl", "go_library")
 
 flatc_path = "@com_github_google_flatbuffers//:flatc"
 
@@ -31,6 +32,10 @@
     "--gen-name-strings",
 ]
 
+DEFAULT_FLATC_GO_ARGS = [
+    "--gen-onefile",
+]
+
 DEFAULT_FLATC_TS_ARGS = [
     "--gen-all",
     "--no-fb-import",
@@ -339,6 +344,42 @@
         deps = ["@com_github_google_flatbuffers//:flatpy"],
     )
 
+def flatbuffer_go_library(
+        name,
+        srcs,
+        importpath,
+        compatible_with = None,
+        target_compatible_with = None,
+        includes = [],
+        include_paths = DEFAULT_INCLUDE_PATHS,
+        flatc_args = DEFAULT_FLATC_GO_ARGS,
+        visibility = None,
+        srcs_filegroup_visibility = None):
+    srcs_lib = "%s_srcs" % (name)
+    outs = ["%s_generated.go" % (s.replace(".fbs", "").split("/")[-1]) for s in srcs]
+    flatc_args = flatc_args + ["--go-namespace", importpath.split("/")[-1]]
+
+    flatbuffer_library_public(
+        name = srcs_lib,
+        srcs = srcs,
+        outs = outs,
+        language_flag = "--go",
+        includes = includes,
+        include_paths = include_paths,
+        flatc_args = flatc_args,
+        compatible_with = compatible_with,
+        target_compatible_with = target_compatible_with,
+    )
+    go_library(
+        name = name,
+        srcs = outs,
+        deps = ["@com_github_google_flatbuffers//go"],
+        importpath = importpath,
+        visibility = visibility,
+        compatible_with = compatible_with,
+        target_compatible_with = target_compatible_with,
+    )
+
 def flatbuffer_ts_library(
         name,
         srcs,