package modindex

import "golang.org/x/tools/internal/modindex"

Package modindex contains code for building and searching an Index of the Go module cache.

Index

Constants

const CurrentVersion int = 0

CurrentVersion tells readers about the format of the index.

Variables

var IndexDir string = func() string {
	var dir string
	if testing.Testing() {
		dir = os.TempDir()
	} else {
		var err error
		dir, err = os.UserCacheDir()

		if err != nil {
			dir = os.TempDir()
		}
	}
	dir = filepath.Join(dir, "goimports")
	if err := os.MkdirAll(dir, 0777); err != nil {
		log.Printf("failed to create modcache index dir: %v", err)
	}
	return dir
}()

IndexDir is where the module index is stored. Each logical index entry consists of a pair of files:

Since the link file is small (<512B), reads and writes to it may be assumed atomic.

Types

type Candidate

type Candidate struct {
	PkgName    string
	Name       string
	Dir        string
	ImportPath string
	Type       LexType
	Deprecated bool
	// information for Funcs
	Results int16   // how many results
	Sig     []Field // arg names and types
}

type Entry

type Entry struct {
	Dir        string // package directory relative to GOMODCACHE; uses OS path separator
	ImportPath string
	PkgName    string
	Version    string
	Names      []string // exported names and information
}

An Entry contains information for an import path.

type Field

type Field struct {
	Arg, Type string
}

type Index

type Index struct {
	Version    int
	GOMODCACHE string    // absolute path of Go module cache dir
	ValidAt    time.Time // moment at which the index was up to date
	Entries    []Entry
}

Index is returned by Read.

func Read

func Read(gomodcache string) (*Index, error)

Read reads the latest version of the on-disk index for the specified Go module cache directory. If there is no index, it returns a nil Index and an fs.ErrNotExist error.

func Update

func Update(gomodcache string) (*Index, error)

Update updates the index for the specified Go module cache directory, creating it as needed. On success it returns the current index.

func (*Index) Lookup

func (ix *Index) Lookup(pkgName, name string, prefix bool) []Candidate

Lookup finds all the symbols in the index with the given PkgName and name. If prefix is true, it finds all of these with name as a prefix.

func (*Index) LookupAll

func (ix *Index) LookupAll(pkgName string, names ...string) map[string][]Candidate

LookupAll only returns those Candidates whose import path finds all the names.

func (*Index) String

func (ix *Index) String() string

type LexType

type LexType int8
const (
	Const LexType = iota
	Var
	Type
	Func
)

Source Files

directories.go index.go lookup.go modindex.go symbols.go

Directories

PathSynopsis
internal/modindex/gomodindexA command for building and maintaining the module cache a.out <flags> <command> <args> The commands are: 'update', which attempts to update an existing index, 'query', which looks up things in the index.
Version
v0.36.0
Published
Aug 7, 2025
Platform
js/wasm
Imports
25 packages
Last checked
3 hours ago

Tools for package owners.