Version: v0.1.5 Latest Latest

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

Go to latest
Published: Jul 13, 2021 License: BSD-3-Clause Imports: 12 Imported by: 0



Package modfile implements a parser and formatter for go.mod files.

The go.mod syntax is described in

The Parse and ParseLax functions both parse a go.mod file and return an abstract syntax tree. ParseLax ignores unknown statements and may be used to parse go.mod files that may have been developed with newer versions of Go.

The File struct returned by Parse and ParseLax represent an abstract go.mod file. File has several methods like AddNewRequire and DropReplace that can be used to programmatically edit a file.

The Format function formats a File back to a byte slice which can be written to a file.



This section is empty.


View Source
var GoVersionRE = lazyregexp.New(`^([1-9][0-9]*)\.(0|[1-9][0-9]*)$`)


func AutoQuote

func AutoQuote(s string) string

AutoQuote returns s or, if quoting is required for s to appear in a go.mod, the quotation of s.

func IsDirectoryPath

func IsDirectoryPath(ns string) bool

IsDirectoryPath reports whether the given path should be interpreted as a directory path. Just like on the go command line, relative paths and rooted paths are directory paths; the rest are module paths.

func ModulePath

func ModulePath(mod []byte) string

ModulePath returns the module path from the gomod file text. If it cannot find a module path, it returns an empty string. It is tolerant of unrelated problems in the go.mod file.

func MustQuote

func MustQuote(s string) bool

MustQuote reports whether s must be quoted in order to appear as a single token in a go.mod line.


type Directory

type Directory struct {
	DiskPath   string // TODO(matloob): Replace uses module.Version for new. Do that here?
	ModulePath string // Module path in the comment.
	Syntax     *modfile.Line

A Directory is a single directory statement.

type WorkFile

type WorkFile struct {
	Go        *modfile.Go
	Directory []*Directory
	Replace   []*modfile.Replace

	Syntax *modfile.FileSyntax

A WorkFile is the parsed, interpreted form of a file.

func ParseWork

func ParseWork(file string, data []byte, fix modfile.VersionFixer) (*WorkFile, error)

Parse parses and returns a file.

file is the name of the file, used in positions and errors.

data is the content of the file.

fix is an optional function that canonicalizes module versions. If fix is nil, all module versions must be canonical (module.CanonicalVersion must return the same string).

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL