本文整理汇总了Golang中path/filepath.Walk函数的典型用法代码示例。如果您正苦于以下问题:Golang Walk函数的具体用法?Golang Walk怎么用?Golang Walk使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Walk函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: supplyFile
/*
*copy 缺少json文件的绘本到目标目录
*/
func supplyFile(jsonPath string, forDir string) {
var fileMap map[string]string = make(map[string]string)
_ = filepath.Walk(jsonPath, func(filename string, info os.FileInfo, err error) error {
if !info.IsDir() {
if _, ok := fileMap[info.Name()]; !ok {
var value = jsonPath + "\\" + info.Name()
fileMap[info.Name()] = value
}
}
return nil
})
_ = filepath.Walk(forDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
var fileName = path + "\\" + info.Name() + ".json"
_, err := os.Stat(fileName)
if err == nil || os.IsExist(err) {
fmt.Println(fileName + " is exist")
} else {
if v, ok := fileMap[info.Name()+".json"]; ok {
// fmt.Println("############# " + v)
if _, err := CopyFile(fileName, v); err != nil {
panic(err)
}
} else {
fmt.Println("~~~~~~~~~~~~~ " + fileName + " is not exist")
}
}
}
return nil
})
//fmt.Printf("%s\n", fileMap)
}
示例2: TagDirParallel
func TagDirParallel(start_dir string) {
//Setup fingerprint worker
for w := 0; w < 3; w++ {
go FingerprintWorker(w)
}
//Setup acousticid worker
for w := 0; w < 3; w++ {
go AcousticidWorker(w)
}
//Setup ID3 worker
for w := 0; w < 3; w++ {
go ID3Worker(w)
}
//Count the number of files to tag:
filepath.Walk(start_dir, countMp3Files)
log.Info("Need to tag %d mp3 files..", mp3FileCount)
//Send the files to be fingerprinted
filepath.Walk(start_dir, tagFileParallel)
//Ensure all files have been processed
for i := 0; i < mp3FileCount; i++ {
<-processedChan
}
}
示例3: Walk
// Walk returns the list of files found in the local repository by scanning the
// file system. Files are blockwise hashed.
func (w *Walker) Walk() (chan protocol.FileInfo, error) {
if debug {
l.Debugln("Walk", w.Dir, w.Sub, w.BlockSize, w.IgnoreFile)
}
err := checkDir(w.Dir)
if err != nil {
return nil, err
}
files := make(chan protocol.FileInfo)
hashedFiles := make(chan protocol.FileInfo)
newParallelHasher(w.Dir, w.BlockSize, runtime.NumCPU(), hashedFiles, files)
var ignores []*regexp.Regexp
go func() {
filepath.Walk(w.Dir, w.loadIgnoreFiles(w.Dir, &ignores))
hashFiles := w.walkAndHashFiles(files, ignores)
filepath.Walk(filepath.Join(w.Dir, w.Sub), hashFiles)
close(files)
}()
return hashedFiles, nil
}
示例4: main
func main() {
var nogo = flag.Bool("nogo", false, "prevent running the 'go build' command")
var f = flag.Bool("f", false, "force compilation of all haml files")
var v = flag.Bool("v", false, "prints the name of the files as they are compiled")
var clean = flag.Bool("clean", false, "cleans generated *.go files")
flag.Parse()
cfg := &ghamlConfig{
goBuildAfter: *nogo == false,
forceCompile: *f,
verbose: *v,
clean: *clean,
}
wdStr, err := os.Getwd()
if err != nil {
panic("Can't get working directory")
}
if cfg.clean {
filepath.Walk(wdStr, makeWalkFunc(checkFileForDeletion, cfg))
return
}
// create closure to pass our config into a WalkFunc
filepath.Walk(wdStr, makeWalkFunc(checkFileForCompilation, cfg))
if cfg.goBuildAfter {
runGoBuild(cfg)
}
}
示例5: Walk
// Walk returns the list of files found in the local repository by scanning the
// file system. Files are blockwise hashed.
func (w *Walker) Walk() (files []File, ignore map[string][]string, err error) {
if debug {
l.Debugln("Walk", w.Dir, w.BlockSize, w.IgnoreFile)
}
err = checkDir(w.Dir)
if err != nil {
return
}
t0 := time.Now()
ignore = make(map[string][]string)
hashFiles := w.walkAndHashFiles(&files, ignore)
filepath.Walk(w.Dir, w.loadIgnoreFiles(w.Dir, ignore))
filepath.Walk(w.Dir, hashFiles)
if debug {
t1 := time.Now()
d := t1.Sub(t0).Seconds()
l.Debugf("Walk in %.02f ms, %.0f files/s", d*1000, float64(len(files))/d)
}
err = checkDir(w.Dir)
return
}
示例6: GodepWorkspace
// GodepWorkspace removes any Godeps/_workspace directories and makes sure
// any rewrites are undone.
// Note, this is not concuccency safe.
func GodepWorkspace(v string) error {
vPath = v
if _, err := os.Stat(vPath); err != nil {
if os.IsNotExist(err) {
msg.Debug("Vendor directory does not exist.")
}
return err
}
err := filepath.Walk(vPath, stripGodepWorkspaceHandler)
if err != nil {
return err
}
// Walk the marked projects to make sure rewrites are undone.
for k := range godepMark {
msg.Info("Removing Godep rewrites for %s", k)
err := filepath.Walk(k, rewriteGodepfilesHandler)
if err != nil {
return err
}
}
return nil
}
示例7: stdlib
func stdlib() {
if len(os.Args) < 4 {
printHelpToStderr()
os.Exit(1)
}
goroot := filepath.Join(os.Args[2], "src", "pkg")
outdir := os.Args[len(os.Args)-1]
fmt.Printf("Building standard library documentation from '%s' to '%s'\n",
goroot, outdir)
filepath.Walk(goroot, dirVisitor(outdir), nil)
for _, pkgroot := range os.Args[3 : len(os.Args)-1] {
fmt.Printf("Building documentation from '%s' to '%s'\n",
pkgroot, outdir)
filepath.Walk(pkgroot, dirVisitor(outdir), nil)
}
fmt.Println("Writing shared data...")
run("gortfm", "-outdir", outdir)
fmt.Println("Writing index page...")
writeIndexPage(outdir)
fmt.Println("Writing index page data...")
writeIndexPageData(outdir)
}
示例8: ProcessZip
func ProcessZip(currentUser m.User, outputZipPath string) {
var imagesFound = 0
var tablesFound = 0
var WalkImageCallback = func(path string, fi os.FileInfo, err error) error {
if err != nil {
return nil
}
if fi.IsDir() {
return nil
}
if !strings.Contains(fi.Name(), ".txt") {
imagesFound++
log.Info(strconv.Itoa(imagesFound))
file, err := os.Open(path)
if err != nil {
// log.Error("Failed to read image file from zip: " + err.Error())
return nil
}
ProcessImage(currentUser, file, nil, true)
}
return nil
}
var WalkITableCallback = func(path string, fi os.FileInfo, err error) error {
if err != nil {
return nil
}
if fi.IsDir() {
return nil
}
if strings.Contains(fi.Name(), ".txt") {
tablesFound++
// log.Info(strconv.Itoa(tablesFound))
file, err := os.Open(path)
if err != nil {
log.Error("Failed to read txt file from zip: " + err.Error())
return nil
}
defer file.Close()
ParseImageTable(currentUser, file)
}
return nil
}
log.Info("Received zip file for processing")
filepath.Walk(outputZipPath, WalkImageCallback)
log.Info("Images found in zip: " + strconv.Itoa(imagesFound))
log.Info("Starting processing of image tables.")
filepath.Walk(outputZipPath, WalkITableCallback)
log.Info("Tables found in zip: " + strconv.Itoa(tablesFound))
os.Remove(outputZipPath)
}
示例9: UndoCmd
func UndoCmd(args []string) (err error) {
if len(args) != 0 {
return errors.New("Usage: gorf [flags] undo")
}
lastChangePath := filepath.Join(LocalRoot, ".change.0.gorfc")
var srcFile *os.File
srcFile, err = os.Open(lastChangePath)
if err != nil {
return
}
buf := make([]byte, 1024)
var n int
n, err = srcFile.Read(buf)
fmt.Printf("Undoing \"%s\"\n", strings.TrimSpace(string(buf[:n])))
filepath.Walk(LocalRoot, undoscanner(0).Walk)
ur := UndoRoller{incr: -1}
filepath.Walk(LocalRoot, ur.Walk)
return ur.err
return
}
示例10: GenErrorInSrc
func GenErrorInSrc(src_path string) {
log.Println(src_path)
//base_dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
//base_dir = base_dir + src_path
//src_path, _ = filepath.Abs(base_dir)
filepath.Walk(src_path, func(path string, info os.FileInfo, err error) error {
// смотрим все файлы *.go, но не _gen.go
if strings.HasSuffix(path, errgen_sfx) {
println("remove: ", path)
os.Remove(path)
}
return nil
})
filepath.Walk(src_path, func(path string, info os.FileInfo, err error) error {
// смотрим все файлы *.go, но не _err.gen.go
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, errgen_sfx) {
return nil
}
// пропускаем папки
for _, skip_path := range skip_paths {
if strings.Contains(path, skip_path) {
return nil
}
}
parse_file(path)
return nil
})
}
示例11: watch
// watch recursively watches changes in root and reports the filenames to names.
// It sends an error on the done chan.
// As an optimization, any dirs we encounter that meet the ExcludePrefix criteria of all reflexes can be
// ignored.
func watch(root string, watcher *fsnotify.Watcher, names chan<- string, done chan<- error, reflexes []*Reflex) {
if err := filepath.Walk(root, walker(watcher, reflexes)); err != nil {
infoPrintf(-1, "Error while walking path %s: %s", root, err)
}
for {
select {
case e := <-watcher.Events:
if verbose {
infoPrintln(-1, "fsnotify event:", e)
}
stat, err := os.Stat(e.Name)
if err != nil {
continue
}
path := normalize(e.Name, stat.IsDir())
if e.Op&chmodMask == 0 {
continue
}
names <- path
if e.Op&fsnotify.Create > 0 && stat.IsDir() {
if err := filepath.Walk(path, walker(watcher, reflexes)); err != nil {
infoPrintf(-1, "Error while walking path %s: %s", path, err)
}
}
// TODO: Cannot currently remove fsnotify watches recursively, or for deleted files. See:
// https://github.com/cespare/reflex/issues/13
// https://github.com/go-fsnotify/fsnotify/issues/40
// https://github.com/go-fsnotify/fsnotify/issues/41
case err := <-watcher.Errors:
done <- err
return
}
}
}
示例12: ObserveDir
func (w *Watcher) ObserveDir() {
error := filepath.Walk(w.Dir, w.walkPopulate)
mirror := new(Watcher)
if error != nil {
log.Println("ON DAEMON: ", error)
return
}
mirror.ModTimes = make(map[string]time.Time, 0)
error = filepath.Walk(w.Dir, mirror.walkPopulate)
if error != nil {
log.Println("ON DAEMON: ", error)
return
}
for {
err := filepath.Walk(w.Dir, w.walkCheck)
if err != nil {
log.Println("ON DAEMON: ", err)
return
}
time.Sleep(time.Second * 2)
}
}
示例13: DiskSize
// DiskSize returns the size on disk of this shard
func (s *Shard) DiskSize() (int64, error) {
var size int64
err := filepath.Walk(s.path, func(_ string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if !fi.IsDir() {
size += fi.Size()
}
return err
})
if err != nil {
return 0, err
}
err = filepath.Walk(s.walPath, func(_ string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if !fi.IsDir() {
size += fi.Size()
}
return err
})
return size, err
}
示例14: main
func main() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
pkgDir := filepath.Clean(filepath.Join(wd, "src", "pkg"))
pkgWalker := &srcWalker{srcDir: pkgDir, pkg: true}
filepath.Walk(pkgDir, pkgWalker, nil)
pkgTargets := pkgWalker.finish()
for pkg, target := range pkgTargets {
makePkg(pkg, target, pkgTargets)
}
makeDirs(pkgDir, pkgTargets)
makeDeps(pkgDir, pkgTargets)
cmdDir := filepath.Clean(filepath.Join(wd, "src", "cmd"))
cmdWalker := &srcWalker{srcDir: cmdDir, pkg: false}
filepath.Walk(cmdDir, cmdWalker, nil)
cmdTargets := cmdWalker.finish()
for cmd, target := range cmdTargets {
makeCmd(cmd, target, pkgTargets)
}
makeDirs(cmdDir, cmdTargets)
makeGitIgnore(cmdDir, cmdTargets)
}
示例15: Walk
// Walk returns the list of files found in the local repository by scanning the
// file system. Files are blockwise hashed.
func (m *Model) Walk(followSymlinks bool) (files []File, ignore map[string][]string) {
ignore = make(map[string][]string)
hashFiles := m.walkAndHashFiles(&files, ignore)
filepath.Walk(m.dir, m.loadIgnoreFiles(ignore))
filepath.Walk(m.dir, hashFiles)
if followSymlinks {
d, err := os.Open(m.dir)
if err != nil {
return
}
defer d.Close()
fis, err := d.Readdir(-1)
if err != nil {
return
}
for _, info := range fis {
if info.Mode()&os.ModeSymlink != 0 {
dir := path.Join(m.dir, info.Name()) + "/"
filepath.Walk(dir, m.loadIgnoreFiles(ignore))
filepath.Walk(dir, hashFiles)
}
}
}
return
}