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 ¶
- func ExcludeFile(pkg *packages.Package, file *ast.File, exclude ...string) bool
- func Filepath(pkg *packages.Package, filename string) string
- func Format(filename string, src []byte, opt *imports.Options) ([]byte, error)
- func Inspect(pkg *packages.Package, f func(n ast.Node) (bool, error), exclude ...string) error
- func Load(cfg *packages.Config, patterns ...string) ([]*packages.Package, error)
- func PrintHeader(w io.Writer, pkg string, imports ...string)
- func Write(filename string, src []byte, opt *imports.Options) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExcludeFile ¶ added in v0.3.8
ExcludeFile returns true if the given file is on the exclude list.
func Filepath ¶
Filepath returns the filepath of a file in the given package with the given filename relative to the package.
func Format ¶
Format returns the given bytes with goimports applied. It wraps imports.Process by wrapping any error with additional context.
func Inspect ¶
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 ¶
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 ¶
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.
Types ¶
This section is empty.