pkgload

package module
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2023 License: MIT Imports: 4 Imported by: 6

README

pkgload

build-img pkg-img reportcard-img version-img

Package pkgload is a set of utilities for go/packages load-related operations.

Installation:

Go version 1.17+

go get github.com/go-toolsmith/pkgload

Example

package main

import (
	"fmt"
	"go/token"

	"github.com/go-toolsmith/pkgload"
	"golang.org/x/tools/go/packages"
)

func main() {
	fset := token.NewFileSet()
	cfg := &packages.Config{
		Mode:  packages.LoadSyntax,
		Tests: true,
		Fset:  fset,
	}

	patterns := []string{"mypackage"}
	pkgs, err := pkgload.LoadPackages(cfg, patterns)
	if err != nil {
		panic(err)
	}

	pkgs = pkgload.Deduplicate(pkgs)

	pkgload.VisitUnits(pkgs, func(u *pkgload.Unit) {
		pkgPath := u.NonNil().PkgPath
		println(pkgPath)
	})
}

License

MIT License.

Documentation

Overview

Package pkgload is a set of utilities for `go/packages` load-related operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deduplicate

func Deduplicate(pkgs []*packages.Package) []*packages.Package

Deduplicate returns a copy of pkgs slice where all duplicated package entries are removed.

Packages are considered equal if all conditions below are satisfied:

  • Same ID
  • Same Name
  • Same PkgPath
  • Equal GoFiles

func LoadPackages added in v1.2.0

func LoadPackages(cfg *packages.Config, patterns []string) ([]*packages.Package, error)

LoadPackages with a given config and patterns.

func VisitUnits

func VisitUnits(pkgs []*packages.Package, visit func(*Unit))

VisitUnits traverses potentially unsorted pkgs list as a set of units. All related packages from the slice are passed into visit func as a single unit. Units are visited in a sorted order (import path).

All packages in a slice must be non-nil.

Types

type Unit

type Unit struct {
	// Base is a standard (normal) package.
	//
	// Note: it can be nil for packages that only have external
	// tests, for example.
	Base *packages.Package

	// Test is a package compiled for test.
	// Can be nil.
	Test *packages.Package

	// ExternalTest is a "_test" compiled package.
	// Can be nil.
	ExternalTest *packages.Package

	// TestBinary is a test binary.
	// Non-nil if Test or ExternalTest are present.
	TestBinary *packages.Package
}

Unit is a set of packages that form a logical group. It is guaranteed that at least 1 field of this object is non-nil.

func (*Unit) NonNil added in v1.0.1

func (u *Unit) NonNil() *packages.Package

NonNil returns first non-nil field (package) of the unit.

  1. If Base is not nil, return Base.
  2. If Test is not nil, return Test.
  3. If ExternalTest is not nil, return ExternalTest.
  4. Otherwise return TestBinary.

If all unit fields are nil, method panics. This should never happen for properly-loaded units.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL