generate

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package generate provides utilities for building code generators in Go. The standard path for a code generator is: Load -> PrintHeader -> Inspect -> Write.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExcludeFile added in v0.3.8

func ExcludeFile(pkg *packages.Package, file *ast.File, exclude ...string) bool

ExcludeFile returns true if the given file is on the exclude list.

func Filepath

func Filepath(pkg *packages.Package, filename string) string

Filepath returns the filepath of a file in the given package with the given filename relative to the package.

func Format

func Format(filename string, src []byte, opt *imports.Options) ([]byte, error)

Format returns the given bytes with goimports applied. It wraps imports.Process by wrapping any error with additional context.

func Inspect

func Inspect(pkg *packages.Package, f func(n ast.Node) (bool, error), exclude ...string) error

Inspect goes through all of the files in the given package, except those listed in the exclude list, and calls the given function on each node. The bool return value from the given function indicates whether to continue traversing down the AST tree of that node and look at its children. If a non-nil error value is returned by the given function, the traversal of the tree is stopped and the error value is returned.

func Load

func Load(cfg *packages.Config, patterns ...string) ([]*packages.Package, error)

Load loads and returns the Go packages named by the given patterns. Load calls packages.Load and ensures that there is at least one package; this means that, if there is a nil error, the length of the resulting packages is guaranteed to be greater than zero.

func PrintHeader

func PrintHeader(w io.Writer, pkg string, imports ...string)

PrintHeader prints a header to the given writer for a generated file in the given package with the given imports. Imports do not need to be set if you are running Format on the code later, but they should be set for any external packages that many not be found correctly by goimports.

func Write

func Write(filename string, src []byte, opt *imports.Options) error

Write writes the given bytes to the given filename after applying goimports using the given options.

Types

This section is empty.

Jump to

Keyboard shortcuts

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