# Go rules

Rules

## Overview The rules should be considered experimental. They support: * libraries * binaries * tests * vendoring They currently do not support (in order of importance): * `//+build` tags * auto generated BUILD files. * C/C++ interoperation (cgo, swig etc.) * race detector * coverage * test sharding ## Setup * Decide on the name of your package, eg. `github.com/joe/project` * Add the following to your WORKSPACE file: ```python load("/tools/build_rules/go/def", "go_repositories") go_repositories() ``` * Add a `BUILD` file to the top of your workspace, containing ```python load("/tools/build_rules/go/def", "go_prefix") go_prefix("github.com/joe/project") ``` * For a library `github.com/joe/project/lib`, create `lib/BUILD`, containing ```python load("/tools/build_rules/go/def", "go_library") go_library( name = "go_default_library", srcs = ["file.go"] ) ``` * Inside your project, you can use this library by declaring a dependency ```python go_binary( ... deps = ["//lib:go_default_library"] ) ``` * In this case, import the library as `github.com/joe/project/lib`. * For vendored libraries, you may depend on `//lib/vendor/github.com/user/project:go_default_library`. Vendored libraries should have BUILD files like normal libraries. * To declare a test, ```python go_test( name = "mytest", srcs = ["file_test.go"], library = ":go_default_library" ) ``` ## FAQ ### Can I still use the `go` tool? Yes, this setup was deliberately chosen to be compatible with the `go` tool. Make sure your workspace appears under ```sh $GOROOT/src/github.com/joe/project/ ``` eg. ```sh mkdir -p $GOROOT/src/github.com/joe/ ln -s my/bazel/workspace $GOROOT/src/github.com/joe/project ``` and it should work. ## Disclaimer These rules are not supported by Google's Go team. ## go\_prefix ```python go_prefix(prefix) ```
Attributes
prefix String, required

Global prefix used to fully quality all Go targets.

In Go, imports are always fully qualified with a URL, eg. github.com/user/project. Hence, a label //foo:bar from within a Bazel workspace must be referred to as github.com/user/project/foo/bar. To make this work, each rule must know the repository's URL. This is achieved, by having all go rules depend on a globally unique target that has a go_prefix transitive info provider.

## go\_library ```python go_library(name, srcs, deps, data) ```
Attributes
name Name, required

A unique name for this rule.

srcs List of labels, required

List of Go .go source files used to build the library

deps List of labels, optional

List of other libraries to linked to this library target

data List of labels, optional

List of files needed by this rule at runtime.

## go\_binary ```python go_binary(name, srcs, deps, data) ```
Attributes
name Name, required

A unique name for this rule.

srcs List of labels, required

List of Go .go source files used to build the binary

deps List of labels, optional

List of other libraries to linked to this binary target

data List of labels, optional

List of files needed by this rule at runtime.

## go\_test ```python go_test(name, srcs, deps, data) ```
Attributes
name Name, required

A unique name for this rule.

srcs List of labels, required

List of Go .go source files used to build the test

deps List of labels, optional

List of other libraries to linked to this test target

data List of labels, optional

List of files needed by this rule at runtime.