Skip to main content

Examples

Build Multiple Go Binaries

// use a go toolchain with a specific version, returns the name of the target
go_tc := go("go1.20.7")

// depend on files using `fs(name, list of globs)`, returns the name of the target
go_files := fs("go_files", ["go.mod", "go.sum", "**/*.go"])

// create a target `register(name, list of deps, task func)`
register("go_download", [go_tc, go_files], func(bc){
sh('go mod download')
})

build_all := []

for _, goos := range ["windows", "darwin", "linux"] {
for _, goarch := range ["amd64", "arm64"] {
name := 'build_{goos}_{goarch}'
build_all.append(name)
register(name, [go_tc, go_files, "go_download"], func(bc) {
goos := goos
goarch := goarch

// Put any outputs in bc.Out to cache them with yabs
sh('GOOS={goos} GOARCH={goarch} go build -o {bc.Out} .')
})
}
}

register("build_all", build_all, func(bc) {
for _, build := range build_all {
// direct dependencies' outputs are available at `bc.GetDep(target)`
bin_loc := bc.GetDep(build)
print(build, bin_loc)
}
})

Run golangci-lint

go_tc := go("go1.20.7")

go_files := fs("go_files", ["go.mod", "go.sum", "**/*.go"])

os.setenv("GOLANGCI_LINT_CACHE", os.getenv("GOPATH")+"/.lint_cache")

register("golangci-lint", [go_tc], func(bc) {
cmd := 'GOBIN={bc.Out} go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.0'
sh(cmd)
})

register("lint", ["golangci-lint", go_tc], func(bc){
lint_bin := bc.GetDep("golangci-lint") + "/golangci-lint"

sh('PATH={bc.GetDep(go_tc)} {lint_bin} run')
})