{"id":27054180,"url":"https://github.com/bazelbuild/rules_go","last_synced_at":"2025-04-05T09:04:39.535Z","repository":{"id":37442698,"uuid":"53954979","full_name":"bazel-contrib/rules_go","owner":"bazel-contrib","description":"Go rules for Bazel","archived":false,"fork":false,"pushed_at":"2025-03-27T02:44:04.000Z","size":16211,"stargazers_count":1407,"open_issues_count":451,"forks_count":687,"subscribers_count":52,"default_branch":"master","last_synced_at":"2025-03-29T14:10:27.231Z","etag":null,"topics":["bazel","bazel-rules","cgo","go","golang"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bazel-contrib.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"bazel-rules-authors-sig"}},"created_at":"2016-03-15T15:15:14.000Z","updated_at":"2025-03-28T07:05:47.000Z","dependencies_parsed_at":"2023-12-29T23:01:18.777Z","dependency_job_id":"918a6b20-e7cf-4062-92ab-c8ed8f00a80f","html_url":"https://github.com/bazel-contrib/rules_go","commit_stats":{"total_commits":2028,"total_committers":299,"mean_commits":6.782608695652174,"dds":0.7657790927021696,"last_synced_commit":"6505cf2e4f0a768497b123a74363f47b711e1d02"},"previous_names":["bazel-contrib/rules_go","bazelbuild/rules_go"],"tags_count":309,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazel-contrib%2Frules_go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazel-contrib%2Frules_go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazel-contrib%2Frules_go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazel-contrib%2Frules_go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bazel-contrib","download_url":"https://codeload.github.com/bazel-contrib/rules_go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247312077,"owners_count":20918344,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bazel","bazel-rules","cgo","go","golang"],"created_at":"2025-04-05T09:02:44.276Z","updated_at":"2025-04-05T09:04:39.505Z","avatar_url":"https://github.com/bazel-contrib.png","language":"Go","readme":"Go rules for Bazel_\n=====================\n\n.. Links to external sites and pages\n.. _//tests/core/cross: https://github.com/bazelbuild/rules_go/blob/master/tests/core/cross/BUILD.bazel\n.. _Avoiding conflicts: proto/core.rst#avoiding-conflicts\n.. _Bazel labels: https://docs.bazel.build/versions/master/build-ref.html#labels\n.. _Bazel: https://bazel.build/\n.. _Bazel Tutorial\\: Build a Go Project: https://bazel.build/start/go\n.. _Build modes: go/modes.rst\n.. _Bzlmod: https://bazel.build/external/overview#bzlmod\n.. _Go with Bzlmod: docs/go/core/bzlmod.md\n.. _Go with WORKSPACE: docs/go/core/workspace.md\n.. _Core rules: docs/go/core/rules.md\n.. _Coverage: https://bazel.google.cn/docs/coverage\n.. _Dependencies: go/dependencies.rst\n.. _Deprecation schedule: https://github.com/bazelbuild/rules_go/wiki/Deprecation-schedule\n.. _examples/basic_gazelle: examples/basic_gazelle\n.. _examples/hello: examples/hello\n.. _Gopher Slack: https://invite.slack.golangbridge.org/\n.. _gopls integration: https://github.com/bazelbuild/rules_go/wiki/Editor-setup\n.. _Overriding dependencies: go/dependencies.rst#overriding-dependencies\n.. _Proto rules: proto/core.rst\n.. _Protocol buffers: proto/core.rst\n.. _Toolchains: go/toolchains.rst\n.. _Using rules_go on Windows: windows.rst\n.. _bazel-go-discuss: https://groups.google.com/forum/#!forum/bazel-go-discuss\n.. _configuration transition: https://docs.bazel.build/versions/master/skylark/lib/transition.html\n.. _gazelle update-repos: https://github.com/bazelbuild/bazel-gazelle#update-repos\n.. _gazelle: https://github.com/bazelbuild/bazel-gazelle\n.. _github.com/bazelbuild/bazel-gazelle: https://github.com/bazelbuild/bazel-gazelle\n.. _github.com/bazelbuild/rules_go/go/tools/bazel: https://pkg.go.dev/github.com/bazelbuild/rules_go/go/tools/bazel?tab=doc\n.. _nogo build-time static analysis: go/nogo.rst\n.. _nogo: go/nogo.rst\n.. _rules_go and Gazelle roadmap: https://github.com/bazelbuild/rules_go/wiki/Roadmap\n.. _#bazel on Go Slack: https://gophers.slack.com/archives/C1SCQE54N\n.. _#go on Bazel Slack: https://bazelbuild.slack.com/archives/CDBP88Z0D\n\n.. Go rules\n.. _go_binary: docs/go/core/rules.md#go_binary\n.. _go_context: go/toolchains.rst#go_context\n.. _go_deps: https://github.com/bazel-contrib/bazel-gazelle/blob/master/extensions.md#go_deps\n.. _go_download_sdk: go/toolchains.rst#go_download_sdk\n.. _go_host_sdk: go/toolchains.rst#go_host_sdk\n.. _go_library: docs/go/core/rules.md#go_library\n.. _go_local_sdk: go/toolchains.rst#go_local_sdk\n.. _go_path: docs/go/core/rules.md#go_path\n.. _go_proto_compiler: proto/core.rst#go_proto_compiler\n.. _go_proto_library: proto/core.rst#go_proto_library\n.. _go_register_toolchains: go/toolchains.rst#go_register_toolchains\n.. _go_repository: https://github.com/bazelbuild/bazel-gazelle/blob/master/repository.md#go_repository\n.. _go_rules_dependencies: go/dependencies.rst#go_rules_dependencies\n.. _go_source: docs/go/core/rules.md#go_source\n.. _go_test: docs/go/core/rules.md#go_test\n.. _go_cross_binary: docs/go/core/rules.md#go_cross_binary\n.. _go_toolchain: go/toolchains.rst#go_toolchain\n.. _go_wrap_sdk: go/toolchains.rst#go_wrap_sdk\n.. _gomock: docs/go/extras/extras.md#gomock\n\n.. External rules\n.. _git_repository: https://docs.bazel.build/versions/master/repo/git.html\n.. _http_archive: https://docs.bazel.build/versions/master/repo/http.html#http_archive\n.. _proto_library: https://github.com/bazelbuild/rules_proto\n\n.. Issues\n.. _#265: https://github.com/bazelbuild/rules_go/issues/265\n.. _#721: https://github.com/bazelbuild/rules_go/issues/721\n.. _#889: https://github.com/bazelbuild/rules_go/issues/889\n.. _#1199: https://github.com/bazelbuild/rules_go/issues/1199\n.. _#2775: https://github.com/bazelbuild/rules_go/issues/2775\n\n\nMailing list: `bazel-go-discuss`_\n\nSlack: `#go on Bazel Slack`_, `#bazel on Go Slack`_\n\nContents\n--------\n\n* `Overview`_\n* `Setup`_\n* `FAQ`_\n\nDocumentation\n~~~~~~~~~~~~~\n\n* `Core rules`_\n\n  * `go_binary`_\n  * `go_library`_\n  * `go_test`_\n  * `go_source`_\n  * `go_path`_\n  * `go_cross_binary`_\n\n* `Proto rules`_\n\n  * `go_proto_library`_\n  * `go_proto_compiler`_\n\n* `Dependencies`_\n\n  * `go_rules_dependencies`_\n  * `go_repository`_ (Gazelle)\n\n* `Toolchains`_\n\n  * `go_register_toolchains`_\n  * `go_download_sdk`_\n  * `go_host_sdk`_\n  * `go_local_sdk`_\n  * `go_wrap_sdk`_\n  * `go_toolchain`_\n  * `go_context`_\n\n* `Extra rules \u003cdocs/go/extras/extras.md\u003e`_\n\n  * `gomock`_\n\n* `nogo build-time static analysis`_\n* `Build modes \u003cgo/modes.rst\u003e`_\n\nQuick links\n~~~~~~~~~~~\n\n* `rules_go and Gazelle roadmap`_\n* `Deprecation schedule`_\n* `Using rules_go on Windows`_\n\nOverview\n--------\n\nThese rules support:\n\n* Building libraries, binaries, and tests (`go_library`_, `go_binary`_,\n  `go_test`_)\n* Go modules via `go_deps`_.\n* Vendoring\n* cgo\n* Cross-compilation\n* Generating BUILD files via gazelle_\n* Build-time static code analysis via nogo_\n* `Protocol buffers`_\n* Remote execution\n* `Coverage`_\n* `gopls integration`_ for editor support\n* Debugging\n\nThey currently do not support or have limited support for:\n\n* C/C++ integration other than cgo (SWIG)\n\nThe Go rules are tested and supported on the following host platforms:\n\n* Linux, macOS, Windows\n* amd64, arm64\n\nUsers have reported success on several other platforms, but the rules are\nonly tested on those listed above.\n\nNote: Since version v0.51.0, rules_go requires Bazel ≥ 6.5.0 to work.\n\nThe ``master`` branch is only guaranteed to work with the latest version of Bazel.\n\n\nSetup\n-----\n\nTo build Go code with Bazel, you will need:\n\n* A recent version of Bazel.\n* A C/C++ toolchain (if using cgo). Bazel will attempt to configure the\n  toolchain automatically.\n* Bash, ``patch``, ``cat``, and a handful of other Unix tools in ``PATH``.\n\nYou normally won't need a Go toolchain installed. Bazel will download one.\n\nSee `Using rules_go on Windows`_ for Windows-specific setup instructions.\nSeveral additional tools need to be installed and configured.\n\nIf you're new to Bazel, read `Bazel Tutorial: Build a Go Project`_, which\nintroduces Bazel concepts and shows you how to set up a small Go workspace to\nbe built with Bazel.\n\nFor a quicker \"hello world\" example, see `examples/hello`_.\n\nFor an example that generates build files and retrieves external dependencies\nusing Gazelle, see `examples/basic_gazelle`_.\n\nFor more detailed `Bzlmod`_ documentation, see `Go with Bzlmod`_.\n\nFor legacy ``WORKSPACE`` instructions, see `Go with WORKSPACE`_.\n\nFAQ\n---\n\n**Go**\n\n* `Can I still use the go command?`_\n* `Does this work with Go modules?`_\n* `What's up with the go_default_library name?`_\n* `How do I cross-compile?`_\n* `How do I access testdata?`_\n* `How do I access go_binary executables from go_test?`_\n\n**Protocol buffers**\n\n* `How do I avoid conflicts with protocol buffers?`_\n* `Can I use a vendored gRPC with go_proto_library?`_\n\n**Dependencies and testing**\n\n* `How do I use different versions of dependencies?`_\n* `How do I test a beta version of the Go SDK?`_\n\nCan I still use the go command?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nYes, but not directly.\n\nrules_go invokes the Go compiler and linker directly, based on the targets\ndescribed with `go_binary`_ and other rules. Bazel and rules_go together\nfill the same role as the ``go`` command, so it's not necessary to use the\n``go`` command in a Bazel workspace.\n\nThat said, it's usually still a good idea to follow conventions required by\nthe ``go`` command (e.g., one package per directory, package paths match\ndirectory paths). Tools that aren't compatible with Bazel will still work,\nand your project can be depended on by non-Bazel projects.\n\nIf you need to use the ``go`` command to perform tasks that Bazel doesn't cover\n(such as adding a new dependency to ``go.mod``), you can use the following Bazel\ninvocation to run the ``go`` binary of the Bazel-configured Go SDK:\n\n.. code:: bash\n\n    bazel run @io_bazel_rules_go//go -- \u003cargs\u003e\n\nPrefer this to running ``go`` directly since it ensures that the version of Go\nis identical to the one used by rules_go.\n\nDoes this work with Go modules?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nYes, but not directly. Bazel ignores ``go.mod`` files, and all package\ndependencies must be expressed through ``deps`` attributes in targets\ndescribed with `go_library`_ and other rules.\n\nYou can download a Go module at a specific version as an external repository\nusing `go_repository`_, a workspace rule provided by gazelle_. This will also\ngenerate build files using gazelle_.\n\nYou can import `go_repository`_ rules from a ``go.mod`` file using\n`gazelle update-repos`_.\n\nWhat's up with the go_default_library name?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThis was used to keep import paths consistent in libraries that can be built\nwith ``go build`` before the ``importpath`` attribute was available.\n\nIn order to compile and link correctly, rules_go must know the Go import path\n(the string by which a package can be imported) for each library. This is now\nset explicitly with the ``importpath`` attribute. Before that attribute existed,\nthe import path was inferred by concatenating a string from a special\n``go_prefix`` rule and the library's package and label name. For example, if\n``go_prefix`` was ``github.com/example/project``, for a library\n``//foo/bar:bar``, rules_go would infer the import path as\n``github.com/example/project/foo/bar/bar``. The stutter at the end is\nincompatible with ``go build``, so if the label name was ``go_default_library``,\nthe import path would not include it. So for the library\n``//foo/bar:go_default_library``, the import path would be\n``github.com/example/project/foo/bar``.\n\nSince ``go_prefix`` was removed and the ``importpath`` attribute became\nmandatory (see `#721`_), the ``go_default_library`` name no longer serves any\npurpose. We may decide to stop using it in the future (see `#265`_).\n\nHow do I cross-compile?\n~~~~~~~~~~~~~~~~~~~~~~~\n\nYou can cross-compile by setting the ``--platforms`` flag on the command line.\nFor example:\n\n.. code::\n\n  $ bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 //cmd\n\nBy default, cgo is disabled when cross-compiling. To cross-compile with cgo,\nadd a ``_cgo`` suffix to the target platform. You must register a\ncross-compiling C/C++ toolchain with Bazel for this to work.\n\n.. code::\n\n  $ bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64_cgo //cmd\n\nPlatform-specific sources with build tags or filename suffixes are filtered\nautomatically at compile time. You can selectively include platform-specific\ndependencies with ``select`` expressions (Gazelle does this automatically).\n\n.. code:: bzl\n\n  go_library(\n      name = \"foo\",\n      srcs = [\n          \"foo_linux.go\",\n          \"foo_windows.go\",\n      ],\n      deps = select({\n          \"@io_bazel_rules_go//go/platform:linux_amd64\": [\n              \"//bar_linux\",\n          ],\n          \"@io_bazel_rules_go//go/platform:windows_amd64\": [\n              \"//bar_windows\",\n          ],\n          \"//conditions:default\": [],\n      }),\n  )\n\nTo build a specific `go_binary`_ target for a target platform or using a\nspecific golang SDK version, use the `go_cross_binary`_ rule. This is useful\nfor producing multiple binaries for different platforms in a single build.\n\nTo build a specific `go_test`_ target for a target platform, set the\n``goos`` and ``goarch`` attributes on that rule.\n\nYou can equivalently depend on a `go_binary`_ or `go_test`_ rule through\na Bazel `configuration transition`_ on ``//command_line_option:platforms``\n(there are problems with this approach prior to rules_go 0.23.0).\n\nHow do I access testdata?\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\nBazel executes tests in a sandbox, which means tests don't automatically have\naccess to files. You must include test files using the ``data`` attribute.\nFor example, if you want to include everything in the ``testdata`` directory:\n\n.. code:: bzl\n\n  go_test(\n      name = \"foo_test\",\n      srcs = [\"foo_test.go\"],\n      data = glob([\"testdata/**\"]),\n      importpath = \"github.com/example/project/foo\",\n  )\n\nBy default, tests are run in the directory of the build file that defined them.\nNote that this follows the Go testing convention, not the Bazel convention\nfollowed by other languages, which run in the repository root. This means\nthat you can access test files using relative paths. You can change the test\ndirectory using the ``rundir`` attribute. See go_test_.\n\nGazelle will automatically add a ``data`` attribute like the one above if you\nhave a ``testdata`` directory *unless* it contains buildable .go files or\nbuild files, in which case, ``testdata`` is treated as a normal package.\n\nNote that on Windows, data files are not directly available to tests, since test\ndata files rely on symbolic links, and by default, Windows doesn't let\nunprivileged users create symbolic links. You can use the\n`github.com/bazelbuild/rules_go/go/tools/bazel`_ library to access data files.\n\nHow do I access go_binary executables from go_test?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe location where ``go_binary`` writes its executable file is not stable across\nrules_go versions and should not be depended upon. The parent directory includes\nsome configuration data in its name. This prevents Bazel's cache from being\npoisoned when the same binary is built in different configurations. The binary\nbasename may also be platform-dependent: on Windows, we add an .exe extension.\n\nTo depend on an executable in a ``go_test`` rule, reference the executable\nin the ``data`` attribute (to make it visible), then expand the location\nin ``args``. The real location will be passed to the test on the command line.\nFor example:\n\n.. code:: bzl\n\n  go_binary(\n      name = \"cmd\",\n      srcs = [\"cmd.go\"],\n  )\n\n  go_test(\n      name = \"cmd_test\",\n      srcs = [\"cmd_test.go\"],\n      args = [\"$(location :cmd)\"],\n      data = [\":cmd\"],\n  )\n\nSee `//tests/core/cross`_ for a full example of a test that\naccesses a binary.\n\nAlternatively, you can set the ``out`` attribute of `go_binary`_ to a specific\nfilename. Note that when ``out`` is set, the binary won't be cached when\nchanging configurations.\n\n.. code:: bzl\n\n  go_binary(\n      name = \"cmd\",\n      srcs = [\"cmd.go\"],\n      out = \"cmd\",\n  )\n\n  go_test(\n      name = \"cmd_test\",\n      srcs = [\"cmd_test.go\"],\n      data = [\":cmd\"],\n  )\n\nHow do I avoid conflicts with protocol buffers?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nSee `Avoiding conflicts`_ in the proto documentation.\n\nCan I use a vendored gRPC with go_proto_library?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThis is not supported. When using `go_proto_library`_ with the\n``@io_bazel_rules_go//proto:go_grpc`` compiler, an implicit dependency is added\non ``@org_golang_google_grpc//:go_default_library``. If you link another copy of\nthe same package from ``//vendor/google.golang.org/grpc:go_default_library``\nor anywhere else, you may experience conflicts at compile or run-time.\n\nIf you're using Gazelle with proto rule generation enabled, imports of\n``google.golang.org/grpc`` will be automatically resolved to\n``@org_golang_google_grpc//:go_default_library`` to avoid conflicts. The\nvendored gRPC should be ignored in this case.\n\nIf you specifically need to use a vendored gRPC package, it's best to avoid\nusing ``go_proto_library`` altogether. You can check in pre-generated .pb.go\nfiles and build them with ``go_library`` rules. Gazelle will generate these\nrules when proto rule generation is disabled (add ``# gazelle:proto\ndisable_global`` to your root build file).\n\nHow do I use different versions of dependencies?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nSee `Overriding dependencies`_ for instructions on overriding repositories\ndeclared in `go_rules_dependencies`_.\n\nHow do I test a beta version of the Go SDK?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nrules_go only supports official releases of the Go SDK. However, you can still\ntest beta and RC versions by passing a ``version`` like ``\"1.16beta1\"`` to\n`go_register_toolchains`_. See also `go_download_sdk`_.\n\n.. code:: bzl\n\n  load(\"@io_bazel_rules_go//go:deps.bzl\", \"go_register_toolchains\", \"go_rules_dependencies\")\n\n  go_rules_dependencies()\n\n  go_register_toolchains(version = \"1.17beta1\")\n","funding_links":["https://opencollective.com/bazel-rules-authors-sig"],"categories":["迁移工具","Additional language support","Repositories"],"sub_categories":["Code ownership"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbazelbuild%2Frules_go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbazelbuild%2Frules_go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbazelbuild%2Frules_go/lists"}