go/build

Guided tour · Go Tooling · pkg.go.dev →

Resolve a Go package on disk given GOPATH/GOROOT, its import path, and the current build context (GOOS, GOARCH, tags).

Usage

Import a package

pkg, err := build.Import("net/http", "", 0)
if err != nil { log.Fatal(err) }
fmt.Println(pkg.Dir, pkg.GoFiles)

Custom context (cross-compile target)

ctx := build.Default
ctx.GOOS = "linux"
ctx.GOARCH = "arm64"
ctx.BuildTags = []string{"integration"}
pkg, _ := ctx.Import("myapp/internal/worker", ".", 0)