本文整理匯總了Golang中golang.org/x/tools/go/loader.Config.ImportPkgs方法的典型用法代碼示例。如果您正苦於以下問題:Golang Config.ImportPkgs方法的具體用法?Golang Config.ImportPkgs怎麽用?Golang Config.ImportPkgs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類golang.org/x/tools/go/loader.Config
的用法示例。
在下文中一共展示了Config.ImportPkgs方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: setPTAScope
func setPTAScope(lconf *loader.Config, scope []string) error {
pkgs := buildutil.ExpandPatterns(lconf.Build, scope)
if len(pkgs) == 0 {
return fmt.Errorf("no packages specified for pointer analysis scope")
}
// The value of each entry in pkgs is true,
// giving ImportWithTests (not Import) semantics.
lconf.ImportPkgs = pkgs
return nil
}
示例2: Main
// Main is like calling the 'gog' program.
func Main(config *loader.Config, args []string) (*Output, error) {
var importUnsafe bool
for _, a := range args {
if a == "unsafe" {
importUnsafe = true
break
}
}
extraArgs, err := config.FromArgs(args, true)
if err != nil {
log.Fatal(err)
}
if len(extraArgs) > 0 {
return nil, fmt.Errorf("extra args after pkgs list")
}
if importUnsafe {
// Special-case "unsafe" because go/loader does not let you load it
// directly.
if config.ImportPkgs == nil {
config.ImportPkgs = make(map[string]bool)
}
config.ImportPkgs["unsafe"] = true
}
prog, err := config.Load()
if err != nil {
return nil, err
}
g := New(prog)
var pkgs []*loader.PackageInfo
pkgs = append(pkgs, prog.Created...)
for _, pkg := range prog.Imported {
pkgs = append(pkgs, pkg)
}
for _, pkg := range pkgs {
if err := g.Graph(pkg); err != nil {
return nil, err
}
}
return &g.Output, nil
}
示例3: reduceScope
// reduceScope is called for one-shot queries that need only a single
// typed package. It attempts to guess the query package from pos and
// reduce the analysis scope (set of loaded packages) to just that one
// plus (the exported parts of) its dependencies. It leaves its
// arguments unchanged on failure.
//
// TODO(adonovan): this is a real mess... but it's fast.
//
func reduceScope(pos string, conf *loader.Config) {
fqpos, err := fastQueryPos(pos)
if err != nil {
return // bad query
}
// TODO(adonovan): fix: this gives the wrong results for files
// in non-importable packages such as tests and ad-hoc packages
// specified as a list of files (incl. the oracle's tests).
_, importPath, err := guessImportPath(fqpos.fset.File(fqpos.start).Name(), conf.Build)
if err != nil {
return // can't find GOPATH dir
}
if importPath == "" {
return
}
// Check that it's possible to load the queried package.
// (e.g. oracle tests contain different 'package' decls in same dir.)
// Keep consistent with logic in loader/util.go!
cfg2 := *conf.Build
cfg2.CgoEnabled = false
bp, err := cfg2.Import(importPath, "", 0)
if err != nil {
return // no files for package
}
// Check that the queried file appears in the package:
// it might be a '// +build ignore' from an ad-hoc main
// package, e.g. $GOROOT/src/net/http/triv.go.
if !pkgContainsFile(bp, fqpos.fset.File(fqpos.start).Name()) {
return // not found
}
conf.TypeCheckFuncBodies = func(p string) bool { return p == importPath }
// Ignore packages specified on command line.
conf.CreatePkgs = nil
conf.ImportPkgs = nil
// Instead load just the one containing the query position
// (and possibly its corresponding tests/production code).
// TODO(adonovan): set 'augment' based on which file list
// contains
_ = conf.ImportWithTests(importPath) // ignore error
}