/go-rules

Golang rules for the Please build system

Primary LanguageGoApache License 2.0Apache-2.0

Go Rules

This repo provides Golang build rules for the Please build system.

Basic usage

First add the plugin to your project. In plugins/BUILD:

plugin_repo(
    name = "go",
    revision="vx.x.x",
)

Then add the plugin config:

[Plugin "go"]
Target = //plugins:go
ImportPath = github.com/example/repo

You can then compile and test go Packages like so:

subinclude("///go//build_defs:go")

go_library(
    name = "lib",
    srcs = ["lib.go"],
    deps = ["//some:package"],
)

go_test(
    name = "lib_test",
    srcs = ["lib_test.go"],
    deps = [
        ":lib",
        # Third party dependencies are added to a subrepo
        "///third_party/go/github.com_stretchr_testify//assert",
        # If you've passed in the packages you need via the install arg (see below) then you can depend on them like so
        "//third_party/go:testify",
    ],
)

You can define third party code using go_get:

subinclude("///go//build_defs:go")

# We can give direct modules a name, and install list so we can reference them nicely as :testify
go_repo(
    name = "testify",
    module = "github.com/stretchr/testify",
    version="v1.8.2",
    # We add the subset of packages we actually depend on here
    install = [
        "assert",
        "require",
    ]
)

# Indirect modules are referenced internally, so we don't have to name them if we don't want to
go_repo(module = "github.com/davecgh/go-spew", version="v1.1.1")
go_repo(module = "github.com/pmezard/go-difflib", version="v1.0.0")
go_repo(module = "github.com/stretchr/objx", version="v0.5.0")
go_repo(module = "gopkg.in/yaml.v3", version="v3.0.1")

To compile a binary, you can use go_binary():

subinclude("///go//build_defs:go")

go_binary(
    name = "bin",
    srcs = ["main.go"],
)

Go is especially well suited to writing command line tools and utilities. Binaries can be ran with plz run, or used as a tool for other custom rules.

WARNING: From Go 1.20, Golang no longer ships the precompiled standard library and thus you MUST use go_toolchain or go_system_toolchain.