Dagger
Search

golang

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

Installation

dagger install github.com/purpleclay/daggerverse/golang@v0.3.0

Entrypoint

Return Type
Golang !
Arguments
NameTypeDescription
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. `NOTE:` Any pre-existing entrypoint will be removed, in favour of raw `go` commands
srcDirectory !a path to a directory containing the source code
Example
dagger -m github.com/purpleclay/daggerverse/golang@1190a20046659b68001a6e16cd5c12fab960b216 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

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

dagger call -m github.com/purpleclay/daggerverse/golang --src . mod-version

Return Type
String !
Example
dagger -m github.com/purpleclay/daggerverse/golang@1190a20046659b68001a6e16cd5c12fab960b216 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()
}

build() 🔗

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

Build a binary from a main.go file located at the project root: dagger call -m github.com/purpleclay/daggerverse/golang --src . build

Build a binary targeting a custom os and architecture: dagger call -m github.com/purpleclay/daggerverse/golang --src . build --os linux --arch arm64

Build a binary from a main.go file located within a cmd folder: dagger call -m github.com/purpleclay/daggerverse/golang --src . build --main cmd/example/main.go --out example

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/purpleclay/daggerverse/golang@1190a20046659b68001a6e16cd5c12fab960b216 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

dagger call -m github.com/purpleclay/daggerverse/golang --src . test

Execute only short running tests ensuring they are shuffled: dagger call -m github.com/purpleclay/daggerverse/golang --src . test --short --shuffle

Execute a single test: `dagger call -m github.com/purpleclay/daggerverse/golang –src . test –run ‘TestSingleFeature’

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
Example
dagger -m github.com/purpleclay/daggerverse/golang@1190a20046659b68001a6e16cd5c12fab960b216 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

dagger call -m github.com/purpleclay/daggerverse/golang --src . bench

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/purpleclay/daggerverse/golang@1190a20046659b68001a6e16cd5c12fab960b216 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

dagger call -m github.com/purpleclay/daggerverse/golang --src . vulncheck

Return Type
String !
Example
dagger -m github.com/purpleclay/daggerverse/golang@1190a20046659b68001a6e16cd5c12fab960b216 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

dagger call -m github.com/purpleclay/daggerverse/golang --src . lint

Return Type
String !
Arguments
NameTypeDefault ValueDescription
formatString "colored-line-number"the type of report that should be generated
Example
dagger -m github.com/purpleclay/daggerverse/golang@1190a20046659b68001a6e16cd5c12fab960b216 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.

dagger call -m github.com/purpleclay/daggerverse/golang --src . format export --path .

Return Type
Directory !
Example
dagger -m github.com/purpleclay/daggerverse/golang@1190a20046659b68001a6e16cd5c12fab960b216 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()
}