本文整理汇总了Golang中go/build.Context.ScanDir方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.ScanDir方法的具体用法?Golang Context.ScanDir怎么用?Golang Context.ScanDir使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/build.Context
的用法示例。
在下文中一共展示了Context.ScanDir方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: scanPackage
func scanPackage(ctxt *build.Context, t *build.Tree, arg, importPath, dir string, stk *importStack) *Package {
// Read the files in the directory to learn the structure
// of the package.
p := &Package{
ImportPath: importPath,
Dir: dir,
Standard: t.Goroot && !strings.Contains(importPath, "."),
t: t,
}
packageCache[dir] = p
packageCache[importPath] = p
info, err := ctxt.ScanDir(dir)
if err != nil {
p.Error = &PackageError{
ImportStack: stk.copy(),
Err: err.Error(),
}
p.Incomplete = true
return p
}
p.info = info
p.Name = info.Package
p.Doc = firstSentence(info.PackageComment.Text())
p.Imports = info.Imports
p.GoFiles = info.GoFiles
p.TestGoFiles = info.TestGoFiles
p.XTestGoFiles = info.XTestGoFiles
p.CFiles = info.CFiles
p.HFiles = info.HFiles
p.SFiles = info.SFiles
p.CgoFiles = info.CgoFiles
p.CgoCFLAGS = info.CgoCFLAGS
p.CgoLDFLAGS = info.CgoLDFLAGS
if info.Package == "main" {
_, elem := filepath.Split(importPath)
p.target = filepath.Join(t.BinDir(), elem)
if ctxt.GOOS == "windows" {
p.target += ".exe"
}
} else {
p.target = filepath.Join(t.PkgDir(), filepath.FromSlash(importPath)+".a")
}
var built time.Time
if fi, err := os.Stat(p.target); err == nil {
built = fi.ModTime()
}
// Build list of full paths to all Go files in the package,
// for use by commands like go fmt.
for _, f := range info.GoFiles {
p.gofiles = append(p.gofiles, filepath.Join(dir, f))
}
for _, f := range info.CgoFiles {
p.gofiles = append(p.gofiles, filepath.Join(dir, f))
}
for _, f := range info.TestGoFiles {
p.gofiles = append(p.gofiles, filepath.Join(dir, f))
}
for _, f := range info.XTestGoFiles {
p.gofiles = append(p.gofiles, filepath.Join(dir, f))
}
sort.Strings(p.gofiles)
srcss := [][]string{
p.GoFiles,
p.CFiles,
p.HFiles,
p.SFiles,
p.CgoFiles,
}
Stale:
for _, srcs := range srcss {
for _, src := range srcs {
if fi, err := os.Stat(filepath.Join(p.Dir, src)); err != nil || fi.ModTime().After(built) {
//println("STALE", p.ImportPath, "needs", src, err)
p.Stale = true
break Stale
}
}
}
importPaths := p.Imports
// Packages that use cgo import runtime/cgo implicitly,
// except runtime/cgo itself.
if len(info.CgoFiles) > 0 && (!p.Standard || p.ImportPath != "runtime/cgo") {
importPaths = append(importPaths, "runtime/cgo")
}
// Everything depends on runtime, except runtime and unsafe.
if !p.Standard || (p.ImportPath != "runtime" && p.ImportPath != "unsafe") {
importPaths = append(importPaths, "runtime")
}
// Record package under both import path and full directory name.
packageCache[dir] = p
packageCache[importPath] = p
//.........这里部分代码省略.........
示例2: scanPackage
func scanPackage(ctxt *build.Context, t *build.Tree, arg, importPath, dir string, stk *importStack, useAllFiles bool) *Package {
// Read the files in the directory to learn the structure
// of the package.
p := &Package{
ImportPath: importPath,
Dir: dir,
Standard: t.Goroot && !strings.Contains(importPath, "."),
t: t,
}
packageCache[dir] = p
packageCache[importPath] = p
ctxt.UseAllFiles = useAllFiles
info, err := ctxt.ScanDir(dir)
useAllFiles = false // flag does not apply to dependencies
if err != nil {
p.Error = &PackageError{
ImportStack: stk.copy(),
Err: err.Error(),
}
// Look for parser errors.
if err, ok := err.(scanner.ErrorList); ok {
// Prepare error with \n before each message.
// When printed in something like context: %v
// this will put the leading file positions each on
// its own line. It will also show all the errors
// instead of just the first, as err.Error does.
var buf bytes.Buffer
for _, e := range err {
buf.WriteString("\n")
buf.WriteString(e.Error())
}
p.Error.Err = buf.String()
}
p.Incomplete = true
return p
}
p.info = info
p.Name = info.Package
p.Doc = doc.Synopsis(info.PackageComment.Text())
p.Imports = info.Imports
p.GoFiles = info.GoFiles
p.TestGoFiles = info.TestGoFiles
p.XTestGoFiles = info.XTestGoFiles
p.CFiles = info.CFiles
p.HFiles = info.HFiles
p.SFiles = info.SFiles
p.CgoFiles = info.CgoFiles
p.CgoCFLAGS = info.CgoCFLAGS
p.CgoLDFLAGS = info.CgoLDFLAGS
if info.Package == "main" {
_, elem := filepath.Split(importPath)
full := ctxt.GOOS + "_" + ctxt.GOARCH + "/" + elem
if t.Goroot && isGoTool[p.ImportPath] {
p.target = filepath.Join(t.Path, "pkg/tool", full)
} else {
if ctxt.GOOS != toolGOOS || ctxt.GOARCH != toolGOARCH {
// Install cross-compiled binaries to subdirectories of bin.
elem = full
}
p.target = filepath.Join(t.BinDir(), elem)
}
if ctxt.GOOS == "windows" {
p.target += ".exe"
}
} else {
dir := t.PkgDir()
// For gccgo, rewrite p.target with the expected library name.
if _, ok := buildToolchain.(gccgoToolchain); ok {
dir = filepath.Join(filepath.Dir(dir), "gccgo", filepath.Base(dir))
}
p.target = buildToolchain.pkgpath(dir, p)
}
var built time.Time
if fi, err := os.Stat(p.target); err == nil {
built = fi.ModTime()
}
// Build list of full paths to all Go files in the package,
// for use by commands like go fmt.
for _, f := range info.GoFiles {
p.gofiles = append(p.gofiles, filepath.Join(dir, f))
}
for _, f := range info.CgoFiles {
p.gofiles = append(p.gofiles, filepath.Join(dir, f))
}
for _, f := range info.TestGoFiles {
p.gofiles = append(p.gofiles, filepath.Join(dir, f))
}
for _, f := range info.XTestGoFiles {
p.gofiles = append(p.gofiles, filepath.Join(dir, f))
}
sort.Strings(p.gofiles)
srcss := [][]string{
p.GoFiles,
//.........这里部分代码省略.........