gomod

package
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gomod reads and edits the XGo-specific parts of a go.mod file.

XGo stores two kinds of extra information in go.mod, both as trailing comments so that the file stays valid for the go command.

A class module marker on a require line tells XGo to load the classfile projects declared by that dependency, for example:

require github.com/goplus/yap v0.8.0 //xgo:class

The legacy //gop:class marker is also recognized. See IsClass and File.AddRequire.

A custom compiler is recorded on the go directive, for example:

go 1.18 // llgo 0.9

See Compiler and File.AddCompiler.

File wraps a golang.org/x/mod/modfile.File to create, load, edit and save a go.mod with these markers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compiler

func Compiler(f *gomodfile.File) *modfile.Compiler

Compiler reads a trailing comment on the go directive that names a custom compiler, for example:

go 1.18 // llgo 0.9
go 1.18 // tinygo 0.32

Leading whitespace after "//" is ignored and the version is trimmed. The first matching comment wins. It returns nil if there is none, or if f has no go directive.

func IsClass

func IsClass(r *gomodfile.Require) bool

IsClass reports whether a require is marked as a class module by a trailing //xgo:class (or the legacy //gop:class) comment. The marker is matched after leading whitespace is trimmed, as a prefix, so "// xgo:class" is accepted too. A require without syntax information is never a class module.

Types

type File

type File struct {
	*modfile.File
}

File wraps a modfile.File and provides higher-level operations for editing go.mod. File is a small value type; copies share the same underlying modfile.File, so edits made through any copy are visible to all of them.

func Create

func Create(gomod, modPath, goVer string) (_ File, err error)

Create creates a new, in-memory go.mod for module modPath with a go directive of goVer. gomod is the file name used later by File.Save; nothing is written to disk until then. It fails if goVer is not a valid Go version; modPath is not validated.

func Load

func Load(gomod string) (_ File, err error)

Load reads and parses the go.mod at the given path. The path is remembered so that File.Save writes back to the same file.

func (File) AddCompiler

func (f File) AddCompiler(compiler, ver string)

AddCompiler records a compiler directive (llgo/tinygo) as a comment on the go directive in go.mod, the way the XGo toolchain reads it (see Compiler). For example, AddCompiler("llgo", "0.9") produces "go 1.18 // llgo 0.9". If there is no go directive, one with the default Go version is added first. Any existing trailing comment on the go directive is replaced.

func (File) AddRequire

func (f File) AddRequire(path, vers string, hasProj bool) error

AddRequire adds a require line for path at version vers, or updates the version if path is already required. When hasProj is set (the dependency declares classfile projects), the require is also marked as a class module with a trailing //xgo:class comment, unless it is already marked (see IsClass). An existing marker is kept even when hasProj is false.

func (File) Save

func (f File) Save() error

Save cleans up and formats go.mod, then writes it to the path given to Create or Load. The compiler directive (llgo/tinygo) lives in the go.mod `go` line suffix, and class module markers in require line suffixes, so both survive formatting.

Jump to

Keyboard shortcuts

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