Documentation ¶
Overview ¶
Package goutil is a collection of utilities for working with the go/* packages and the go tool.
Goutil makes it easier to find, parse, and analyze Go code. It also enables easy use of the go tool for running generated code and creating executables from it. There are many miscellaneous utilities for easing the use of the go/* packages.
DocParse has been extracted from the go/doc package as this functionality is not exported.
Importing ¶
There are five ways to import packages with goutil: Import, ImportTree, ImportAll, ImportRec, and the ImportDeps method on *Package. The latter are wrappers around Import for common tasks. Import's documentation applies to all of them, unless otherwise specified.
Imported packages are cached with a pointer to its build.Context as part of the key. The same pointer is also stored on each Package. This means you should never modify a build.Context after using it with this library. If the ctx parameter to any Import function is nil, a pointer to the go/build default context is used.
Packages ¶
A *Package always has its go/build Context and Package set. It has methods to parse the files designated by its build.Package with go/ast and go/doc.
With the exception of Import, the other Import functions all return Packages, a []*Package with methods for filter and map applications.
Some methods of Package and Packages require that certain parsing actions be taken first, but these are always documented.
Gostrap ¶
Gostrap is a utility for running the go(1) command in a temporary directory.
Index ¶
- Constants
- func Context(tags ...string) *build.Context
- func Install(name, location string, file []byte, tags ...string) error
- func Run(file []byte, tags ...string) error
- func TagsOf(c *build.Context) []string
- func ToImport(path string) (root, imp string, err error)
- func WithGostrap(f func(*Gostrap) error) error
- type Block
- type Decls
- type Gostrap
- type Package
- func (p *Package) ASTFilesMatching(tags ...string) (files []*ast.File)
- func (p *Package) Decls() (ds Decls)
- func (p *Package) FilesMatching(tags ...string) (files []string)
- func (p *Package) ImportDeps() (pkgs Packages, err error)
- func (p *Package) Parse(parseComments bool) error
- func (p *Package) ParseDocs(mode doc.Mode) error
- func (p *Package) ParseTags() error
- type Packages
- func (ps Packages) Filter(f func(*Package) bool) (out Packages)
- func (ps Packages) HasFilesMatching(tags ...string) Packages
- func (ps Packages) Imports() (out []string)
- func (ps Packages) NoStdlib() Packages
- func (ps Packages) Parse(parseComments bool) (err error)
- func (ps Packages) ParseDocs(mode doc.Mode) (err error)
- func (ps Packages) ParseTags() (err error)
- func (ps Packages) Uniq() (out Packages)
- type PrefixMatcher
- type StringMatcher
- Bugs
Constants ¶
const ( //Para marks a block as a paragraph. Para op = iota //Head mars a block as a heading. Head //Pre marks a block as pre-formatted text, ie code. Pre )
Variables ¶
This section is empty.
Functions ¶
func Context ¶
Context returns a *build.Context with the appropriate tags.
This does not change GOARCH or GOOS, it only sets additional tags.
If no tags are specified, the default context is returned.
func Install ¶
Install calls go install on a single input file, with optional build tags, in a Gostrap that's created on the fly and destroyed after.
func Run ¶
Run calls go run on a single input file, with optional build tags, in a Gostrap that's created on the fly and destroyed after.
func ToImport ¶
ToImport takes an arbitrary path and returns a valid import path using $GOPATH. Returns which $GOPATH and the import path.
ToImport iterates $GOPATH in the order it's defined and always returns the first match.
Works correctly with imports in the Go root.
Does not handle the special glob ... (see ImportTree).
ToImport handles relative paths and "." The empty string is treated the same as "."
func WithGostrap ¶
WithGostrap creates a new Gostrap, calls Chdir, and defers calls to PopDir and Destroy, in the appropriate order, before invoking f.
Types ¶
type Block ¶
type Block struct { Kind op Lines []string }
A Block is a section of documentation parsed in the godoc format. Each block is either a paragraph (Para), a header (Head), or section of pre-formatted text (Pre), most likely code.
type Decls ¶
Decls is a list ast.Decls.
func (Decls) Named ¶
func (ds Decls) Named(m StringMatcher) (out Decls)
Named returns all Decls whose name matches r.
If you haven't called SplitSpecs, a GenDecl will be returned if any of its Spec's match.
func (Decls) SplitSpecs ¶
SplitSpecs goes throught each *ast.GenDecl and duplicates the outer GenDecl for each item in the spec. This destroys information but does make it easier to search, and is required for invoking Named on GenDecls.
Note that SplitSpecs does not, and in general cannot sensibly, split
var a, b, c = f()
though it does split
var a, b, c = 1, 2, 3
as one would hope.
type Gostrap ¶
Gostrap creates a temporary environment to run the go(1) tool in.
It embeds an exec.Cmd configured to run the go tool in this environment.
func NewGostrap ¶
NewGostrap creates a new Gostrap.
The embedded exec.Cmd still needs the tool (ie, run, install, etc) and its arguments appended to its Args list. It defaults to using os.Stdout and os.Stderr for the Stdout and Stderr of the Cmd, respectively.
You should investigate Run and Install first, as they are likely what you need, and if not their source, and WithGostrap's, show how Gostrap is used.
func (*Gostrap) Chdir ¶
Chdir makes the temp location the current working directory. It saves the current directory so this can be undone with Popdir.
func (*Gostrap) Destroy ¶
func (t *Gostrap) Destroy()
Destroy the Gostrap and everything in it and everything it loves.
type Package ¶
type Package struct { //The context this Package was imported with. Context *build.Context Build *build.Package AST *ast.Package FileSet *token.FileSet //The FileSet AST was parsed with. Doc *doc.Package // contains filtered or unexported fields }
Package wraps all the common go/* Package types and provides some additional functionality.
All methods of package assume that the Package was created by either Import or ImportAll and hence that Build is nonnil.
func Import ¶
Import imports a package.
path is run through ToImport.
If ctx is nil, the default context is used.
N.B. we require a pointer to a build.Context for caching. Two build contexts with identical values that are not represented by the same pointer will have all packages imported by them cached separately.
func (*Package) ASTFilesMatching ¶
ASTFilesMatching calls FilesMatching and looks up the resulting files in p.AST.Files.
It is the callers responsibility to call Parse and ParseTags before invoking this method.
func (*Package) Decls ¶
Decls returns every ast.Decl in a package that is not a BadDecl or an IMPORT.
It is up to the caller to call Parse before invoking this method.
func (*Package) FilesMatching ¶
FilesMatching returns the sublist of Build.GoFiles matching the specified build tags.
It is the users responsibility to call ParseTags before invoking this method.
func (*Package) ImportDeps ¶
ImportDeps imports all dependencies of this package recursively.
It uses the same build.Context this Package was built with.
func (*Package) Parse ¶
Parse the package and set p.AST.
It is not necessary to call with parseComments if you intend to call ParseDocs, as ParseDocs creates its own parse.
func (*Package) ParseDocs ¶
ParseDocs parses the Package's documentation with go/doc.
If you do not need a particular doc.Mode call this with 0.
If the package directory contains a file of package documentation (and the package is not itself named documentation), it is parsed and its doc.Package.Doc string replaces the string generated by the package itself.
Note that the go/doc package munges the AST so this method parses the AST again, regardless of the value in p.AST. As a consequence, it is valid to call this even if you have not called the Parse method or if you have called the Parse method and told it not to parse comments.
type Packages ¶
type Packages []*Package
Packages is a list of *Package, with methods for filtering and manipulating en masse.
func ImportAll ¶
ImportAll imports every package from the standard library and every $GOPATH.
If there are any errors, the first is returned and as many packages as can be imported are returned.
func ImportRec ¶
ImportRec calls Import on path and then ImportDeps and returns all packages with the package described by path as the first element.
func ImportTree ¶
ImportTree imports every package in the directory tree rooted at root. Root need not have a valid package.
If there are any errors, the first is returned and as many packages as can be imported are returned.
func (Packages) Filter ¶
Filter returns a sublist of packages that match the predicate f.
If the predicate requires the Packages to be parsed or have their docs parsed, it is up to the user to ensure that these steps have been done first.
func (Packages) HasFilesMatching ¶
HasFilesMatching returns all packages that have at least one file matching the specified build tags.
HasFilesMatching requires that ParseTags has been invoked.
func (Packages) Imports ¶
Imports return the set of imports in the packages, in no particular order.
func (Packages) NoStdlib ¶
NoStdlib filters out packages from GOROOT.
This only relies on information in Build, so it is safe to call if the packages have not been parsed.
func (Packages) Parse ¶
Parse parses each package in turn.
Parse stops and returns the first error it encounters.
func (Packages) ParseDocs ¶
ParseDocs parses each package's documentation in turn.
ParseDocs stops and returns the first error it encounters.
type PrefixMatcher ¶
type PrefixMatcher string
PrefixMatcher matches all strings with specified prefix.
func (PrefixMatcher) MatchString ¶
func (p PrefixMatcher) MatchString(s string) bool
MatchString matches strings with the prefix set
Notes ¶
Bugs ¶
Tag parser does not handle invalid build tag sequence ,,
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Declgrep greps just the declarations, exported and unexported, excluding import declarations, of a given Go package, or set of Go packages, with the standard build tags.
|
Declgrep greps just the declarations, exported and unexported, excluding import declarations, of a given Go package, or set of Go packages, with the standard build tags. |
Package gocli implements common idioms for handling Go packages in command line programs.
|
Package gocli implements common idioms for handling Go packages in command line programs. |