pkgload

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2019 License: MIT Imports: 4 Imported by: 6

README

Go Report Card GoDoc

pkgload

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

Installation:

go get -v 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 := packages.Load(cfg, patterns...)
	if err != nil {
		return nil, err
	}
	result := pkgs[:0]
	pkgload.VisitUnits(pkgs, func(u *pkgload.Unit) {
		if u.ExternalTest != nil {
			result = append(result, u.ExternalTest)
		}
		result = append(result, u.Base)
	})
}

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 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.
	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.

Jump to

Keyboard shortcuts

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