mod

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2025 License: MIT Imports: 14 Imported by: 1

README

Mod

Go Reference

A package for finding and manipulating go.mod files.

Features

  • Recursively traverses up the filesystem looking for go.mod
  • Supports resolving import paths to directories
  • Supports resolving directories to import paths
  • Extracted from Bud

Install

go get github.com/livebud/mod

Example

func main() {
  module, err := mod.Find(".")
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }
  fmt.Println(module.Dir())
  fmt.Println(module.Import())
}

Contributors

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFileNotFound = fmt.Errorf(`mod: unable to find "go.mod": %w`, fs.ErrNotExist)

ErrFileNotFound occurs when no go.mod can be found

View Source
var GOPATH = build.Default.GOPATH

GOPATH is the default GOPATH for the current build environment. This is exposed for testing purposes, but should not be changed in production code.

Functions

func Abs added in v0.0.3

func Abs(dir string) (absDir string, err error)

Abs finds the absolute directory that contains the go.mod file

func InStdlib added in v0.0.5

func InStdlib(importPath string) bool

InStdlib checks if the import path is from the standard library.

func Infer added in v0.0.7

func Infer(dir string) string

Infer the module path from the $GOPATH. This only works if you work inside $GOPATH otherwise returns an empty string.

func Lookup

func Lookup(dir string) (path string, err error)

Lookup finds the absolute path of the go.mod file in the given directory

Types

type Module

type Module struct {
	fs.FS
	// contains filtered or unexported fields
}

func Find

func Find(dirs ...string) (*Module, error)

Find the first go.mod file in one of the directories below or return an error. Find will also search parent directories for a go.mod file.

func MustFind added in v0.0.3

func MustFind(dirs ...string) *Module

MustFind a go.mod file in this directory or any parent directory. If no go.mod file is found, this will panic.

func New

func New(dir string) *Module

New module

func Parse

func Parse(path string, data []byte) (*Module, error)

Parse a go.mod file

func (*Module) Contains added in v0.0.4

func (m *Module) Contains(importPath string) bool

Contains checks if the module contains the import path.

func (*Module) Dir added in v0.0.3

func (m *Module) Dir(subpaths ...string) string

Dir returns the absolute directory to the module.

func (*Module) Import

func (m *Module) Import(subpaths ...string) string

Import returns the base import path of the module.

func (*Module) ResolveDir added in v0.0.3

func (m *Module) ResolveDir(importPath string) (dir string, err error)

ResolveDir resolves an import path to an absolute path.

func (*Module) ResolveImport added in v0.0.3

func (m *Module) ResolveImport(dir string) (importPath string, err error)

ResolveImport returns an import path from a local directory.

Jump to

Keyboard shortcuts

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