Dagger
Search

golang

A collection of functions for building, formatting, testing, linting and scanning
your Go project for vulnerabilities.

Installation

dagger install github.com/disaster37/dagger-library-go/golang@v0.0.24

Entrypoint

Return Type
Golang !
Arguments
NameTypeDefault ValueDescription
baseContainer -A custom base image containing an installation of golang. If no image is provided, one is resolved based on the Go version defined within the projects go.mod file. The official Go image is pulled from DockerHub using either the bullseye (< 1.20) or bookworm (> 1.20) variants.
srcDirectory !-a path to a directory containing the source code
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH
func (m *myModule) example(src *Directory) *Golang  {
	return dag.
			Golang(src)
}
@function
def example(src: dagger.Directory) -> dag.Golang:
	return (
		dag.golang(src)
	)
@func()
example(src: Directory): Golang {
	return dag
		.golang(src)
}

Types

Golang 🔗

Golang dagger module

container() 🔗

Base is the image used by all golang dagger functions, defaults to the bookworm base image

Return Type
Container !
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH container
func (m *myModule) example(src *Directory) *Container  {
	return dag.
			Golang(src).
			Container()
}
@function
def example(src: dagger.Directory) -> dagger.Container:
	return (
		dag.golang(src)
		.container()
	)
@func()
example(src: Directory): Container {
	return dag
		.golang(src)
		.container()
}

modVersion() 🔗

Echoes the version of go defined within a projects go.mod file. It expects the go.mod file to be located within the root of the project

Return Type
String !
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH mod-version
func (m *myModule) example(ctx context.Context, src *Directory) string  {
	return dag.
			Golang(src).
			ModVersion(ctx)
}
@function
async def example(src: dagger.Directory) -> str:
	return await (
		dag.golang(src)
		.mod_version()
	)
@func()
async example(src: Directory): Promise<string> {
	return dag
		.golang(src)
		.modVersion()
}

withPrivate() 🔗

Enable private Go module support by dynamically constructing a .netrc auto-login configuration file. Each call will append a new auto-login configuration

Return Type
Golang !
Arguments
NameTypeDefault ValueDescription
machineString !-the remote machine name
usernameSecret !-a user on the remote machine that can login
passwordSecret !-a token (or password) used to login into a remote machine by the identified user
modules[String ! ] !-a list of Go module paths that will be treated as private by Go through the GOPRIVATE environment variable
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH with-private --machine string --username env:MYSECRET --password env:MYSECRET --modules string1 --modules string2
func (m *myModule) example(src *Directory, machine string, username *Secret, password *Secret, modules []string) *Golang  {
	return dag.
			Golang(src).
			WithPrivate(machine, username, password, modules)
}
@function
def example(src: dagger.Directory, machine: str, username: dagger.Secret, password: dagger.Secret, modules: List[str]) -> dag.Golang:
	return (
		dag.golang(src)
		.with_private(machine, username, password, modules)
	)
@func()
example(src: Directory, machine: string, username: Secret, password: Secret, modules: string[]): Golang {
	return dag
		.golang(src)
		.withPrivate(machine, username, password, modules)
}

withPrivateLoad() 🔗

Enable private Go module support by loading an existing .netrc auto-login configuration file. Each call will append a new auto-login configuration

Return Type
Golang !
Arguments
NameTypeDefault ValueDescription
cfgFile !-a path to a .netrc auto-login configuration file
modules[String ! ] !-a list of Go module paths that will be treated as private by Go through the GOPRIVATE environment variable
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH with-private-load --cfg file:path --modules string1 --modules string2
func (m *myModule) example(src *Directory, cfg *File, modules []string) *Golang  {
	return dag.
			Golang(src).
			WithPrivateLoad(cfg, modules)
}
@function
def example(src: dagger.Directory, cfg: dagger.File, modules: List[str]) -> dag.Golang:
	return (
		dag.golang(src)
		.with_private_load(cfg, modules)
	)
@func()
example(src: Directory, cfg: File, modules: string[]): Golang {
	return dag
		.golang(src)
		.withPrivateLoad(cfg, modules)
}

build() 🔗

Build a static binary from a Go project using the provided configuration. A directory is returned containing the built binary.

Return Type
Directory !
Arguments
NameTypeDefault ValueDescription
mainString -the path to the main.go file of the project
outString -the name of the built binary
osString -the target operating system
archString -the target architecture
ldflags[String ! ] ["-s", "-w"]flags to configure the linking during a build, by default sets flags for generating a release binary
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH build
func (m *myModule) example(src *Directory) *Directory  {
	return dag.
			Golang(src).
			Build()
}
@function
def example(src: dagger.Directory) -> dagger.Directory:
	return (
		dag.golang(src)
		.build()
	)
@func()
example(src: Directory): Directory {
	return dag
		.golang(src)
		.build()
}

test() 🔗

Execute tests defined within the target project, ignores benchmarks by default

Return Type
String !
Arguments
NameTypeDefault ValueDescription
shortBoolean trueif only short running tests should be executed
shuffleBoolean trueif the tests should be executed out of order
runString -run select tests only, defined using a regex
skipString -skip select tests, defined using a regex
withGotestsumBoolean -Run test with gotestsum
pathString -Path to test
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH test
func (m *myModule) example(ctx context.Context, src *Directory) string  {
	return dag.
			Golang(src).
			Test(ctx)
}
@function
async def example(src: dagger.Directory) -> str:
	return await (
		dag.golang(src)
		.test()
	)
@func()
async example(src: Directory): Promise<string> {
	return dag
		.golang(src)
		.test()
}

bench() 🔗

Execute benchmarks defined within the target project, excludes all other tests

Return Type
String !
Arguments
NameTypeDefault ValueDescription
memoryBoolean trueprint memory allocation statistics for benchmarks
timeString "5s"the time.Duration each benchmark should run for
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH bench
func (m *myModule) example(ctx context.Context, src *Directory) string  {
	return dag.
			Golang(src).
			Bench(ctx)
}
@function
async def example(src: dagger.Directory) -> str:
	return await (
		dag.golang(src)
		.bench()
	)
@func()
async example(src: Directory): Promise<string> {
	return dag
		.golang(src)
		.bench()
}

vulncheck() 🔗

Scans the target project for vulnerabilities using govulncheck

Return Type
String !
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH vulncheck
func (m *myModule) example(ctx context.Context, src *Directory) string  {
	return dag.
			Golang(src).
			Vulncheck(ctx)
}
@function
async def example(src: dagger.Directory) -> str:
	return await (
		dag.golang(src)
		.vulncheck()
	)
@func()
async example(src: Directory): Promise<string> {
	return dag
		.golang(src)
		.vulncheck()
}

lint() 🔗

Lint the target project using golangci-lint

Return Type
String !
Arguments
NameTypeDefault ValueDescription
formatString "colored-line-number"the type of report that should be generated
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH lint
func (m *myModule) example(ctx context.Context, src *Directory) string  {
	return dag.
			Golang(src).
			Lint(ctx)
}
@function
async def example(src: dagger.Directory) -> str:
	return await (
		dag.golang(src)
		.lint()
	)
@func()
async example(src: Directory): Promise<string> {
	return dag
		.golang(src)
		.lint()
}

format() 🔗

Format the source code within a target project using gofumpt. Formatted code must be copied back onto the host.`

Return Type
Directory !
Example
dagger -m github.com/disaster37/dagger-library-go/golang@2d35aed06292796c4d2351b6336caa1f37247f7b call \
 --src DIR_PATH format
func (m *myModule) example(src *Directory) *Directory  {
	return dag.
			Golang(src).
			Format()
}
@function
def example(src: dagger.Directory) -> dagger.Directory:
	return (
		dag.golang(src)
		.format()
	)
@func()
example(src: Directory): Directory {
	return dag
		.golang(src)
		.format()
}