當前位置: 首頁>>代碼示例>>Golang>>正文


Golang fs.Walk函數代碼示例

本文整理匯總了Golang中github.com/kr/fs.Walk函數的典型用法代碼示例。如果您正苦於以下問題:Golang Walk函數的具體用法?Golang Walk怎麽用?Golang Walk使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Walk函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: expandBundlePath

// expandBundlePath calls filepath.Glob first, then recursively adds
// descendents of any directories specified.
func expandBundlePath(pathGlob string) ([]string, error) {
	paths, err := filepath.Glob(pathGlob)
	if err != nil {
		return nil, err
	}

	for _, path := range paths {
		fi, err := os.Stat(path)
		if err != nil {
			return nil, err
		}

		if fi.Mode().IsDir() {
			w := fs.Walk(path)
			for w.Step() {
				if err := w.Err(); err != nil {
					return nil, err
				}
				paths = append(paths, w.Path())
			}
		}
	}

	return paths, nil
}
開發者ID:ildarisaev,項目名稱:srclib,代碼行數:27,代碼來源:bundle.go

示例2: main

func main() {

	if len(os.Args) > 1 {
		starting_path = os.Args[1]
	}

	w := new(tabwriter.Writer)
	w.Init(os.Stdout, 2, 0, 1, ' ', tabwriter.AlignRight)

	walker := fs.Walk(starting_path)
	for walker.Step() {
		if err := walker.Err(); err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
		path := walker.Path()
		if REGEX_EXCLUDE_PATH.FindStringSubmatch(path) != nil {
			continue
		}

		info := walker.Stat()
		file_type := "f"
		if info.IsDir() {
			file_type = "d"
		}

		fmt.Fprintln(w, file_type+" \t"+(info.ModTime().Format(DATE_LAYOUT)+" \t"+strconv.FormatInt(info.Size(), 10)+"\t\t"+path))
	}
	w.Flush()
}
開發者ID:aikawad,項目名稱:docker-workshop,代碼行數:30,代碼來源:walk.go

示例3: Walk

func (w *Walker) Walk() fusefs.Tree {
	wg := sync.WaitGroup{}

	paths := make(chan string, runtime.NumCPU())
	for i := 0; i < runtime.NumCPU(); i++ {
		go worker(w, paths, &wg)
	}

	walker := fs.Walk(w.Path)
	for walker.Step() {
		if err := walker.Err(); err != nil {
			continue
		}

		if walker.Stat().IsDir() {
			continue
		}

		wg.Add(1)
		paths <- walker.Path()
	}

	close(paths)
	wg.Wait()

	return w.tree
}
開發者ID:solarnz,項目名稱:jpgfs,代碼行數:27,代碼來源:walker.go

示例4: RewriteImports

func RewriteImports(path string, rw func(string) string, filter func(string) bool) error {
	w := fs.Walk(path)
	for w.Step() {
		rel := w.Path()[len(path):]
		if len(rel) == 0 {
			continue
		}
		rel = rel[1:]

		if strings.HasPrefix(rel, ".git") || strings.HasPrefix(rel, "vendor") {
			w.SkipDir()
			continue
		}

		if !strings.HasSuffix(w.Path(), ".go") {
			continue
		}

		if !filter(rel) {
			continue
		}

		err := rewriteImportsInFile(w.Path(), rw)
		if err != nil {
			fmt.Println("rewrite error: ", err)
		}
	}
	return nil
}
開發者ID:whyrusleeping,項目名稱:gx-go,代碼行數:29,代碼來源:rewrite.go

示例5: copySrc

func copySrc(dir string, g *Godeps) error {
	ok := true
	for _, dep := range g.Deps {
		w := fs.Walk(dep.dir)
		for w.Step() {
			if w.Err() != nil {
				log.Println(w.Err())
				ok = false
				continue
			}
			if c := w.Stat().Name()[0]; c == '.' || c == '_' {
				// Skip directories using a rule similar to how
				// the go tool enumerates packages.
				// See $GOROOT/src/cmd/go/main.go:/matchPackagesInFs
				w.SkipDir()
			}
			if w.Stat().IsDir() {
				continue
			}
			dst := filepath.Join(dir, w.Path()[len(dep.ws)+1:])
			if err := copyFile(dst, w.Path()); err != nil {
				log.Println(err)
				ok = false
			}
		}
	}
	if !ok {
		return errors.New("error copying source code")
	}
	return nil
}
開發者ID:pedantic-git,項目名稱:godep,代碼行數:31,代碼來源:save.go

示例6: TestBug3486

func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486
	root, err := filepath.EvalSymlinks(runtime.GOROOT())
	if err != nil {
		t.Fatal(err)
	}
	lib := filepath.Join(root, "lib")
	src := filepath.Join(root, "src")
	seenSrc := false
	walker := fs.Walk(root)
	for walker.Step() {
		if walker.Err() != nil {
			t.Fatal(walker.Err())
		}

		switch walker.Path() {
		case lib:
			walker.SkipDir()
		case src:
			seenSrc = true
		}
	}
	if !seenSrc {
		t.Fatalf("%q not seen", src)
	}
}
開發者ID:himanshugpt,項目名稱:evergreen,代碼行數:25,代碼來源:walk_test.go

示例7: copySrc

func copySrc(dir string, deps []Dependency) error {
	ok := true
	for _, dep := range deps {
		srcdir := filepath.Join(dep.ws, "src")
		rel, err := filepath.Rel(srcdir, dep.dir)
		if err != nil { // this should never happen
			return err
		}
		dstpkgroot := filepath.Join(dir, rel)
		err = os.RemoveAll(dstpkgroot)
		if err != nil {
			log.Println(err)
			ok = false
		}
		w := fs.Walk(dep.dir)
		for w.Step() {
			err = copyPkgFile(dir, srcdir, w)
			if err != nil {
				log.Println(err)
				ok = false
			}
		}
	}
	if !ok {
		return errors.New("error copying source code")
	}
	return nil
}
開發者ID:maxamillion,項目名稱:godep,代碼行數:28,代碼來源:save.go

示例8: marcher

// Walk through root and hash each file, return a map with
// filepath for key and hash for value, check only file not dir
func marcher(dir string, method string) map[string]string {
	fmt.Println("")
	fmt.Println("Scanner Report")
	fmt.Println("==============")
	fmt.Println("")
	res := make(map[string]string)
	walker := fs.Walk(dir)
	for walker.Step() {
		// Start walking
		if err := walker.Err(); err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
		// Check if it is a file
		finfo, err := os.Stat(walker.Path())
		if err != nil {
			fmt.Println(err)
			continue
		}
		if finfo.IsDir() {
			// it's a dir so pass and continue
			continue
		} else {
			// it's a file so add couple to data map
			path := walker.Path()
			hash := hasher(walker.Path(), method)
			res[path] = hash
			fmt.Println(hash, "\t", path)
		}
	}
	fmt.Println("")
	return res
}
開發者ID:jhautefeuille,項目名稱:kdouble,代碼行數:35,代碼來源:kdouble.go

示例9: readArticles

func readArticles() ([]*Article, []string, error) {
	timeStart := time.Now()
	walker := fs.Walk("blog_posts")
	var res []*Article
	var dirs []string
	for walker.Step() {
		if walker.Err() != nil {
			fmt.Printf("readArticles: walker.Step() failed with %s\n", walker.Err())
			return nil, nil, walker.Err()
		}
		st := walker.Stat()
		path := walker.Path()
		if st.IsDir() {
			dirs = append(dirs, path)
			continue
		}
		a, err := readArticle(path)
		if err != nil {
			fmt.Printf("readArticle() of %s failed with %s\n", path, err)
			return nil, nil, err
		}
		if a != nil {
			res = append(res, a)
		}
	}
	fmt.Printf("read %d articles in %s\n", len(res), time.Since(timeStart))
	return res, dirs, nil
}
開發者ID:kjk,項目名稱:web-blog,代碼行數:28,代碼來源:store.go

示例10: RewriteImports

func RewriteImports(path string, rw func(string) string, filter func(string) bool) error {
	w := fs.Walk(path)
	for w.Step() {
		rel := w.Path()[len(path):]
		if len(rel) == 0 {
			continue
		}
		rel = rel[1:]

		if strings.HasPrefix(rel, ".git") || strings.HasPrefix(rel, "vendor") {
			w.SkipDir()
			continue
		}

		if !filter(rel) {
			continue
		}

		dir, fi := filepath.Split(w.Path())
		good, err := build.Default.MatchFile(dir, fi)
		if err != nil {
			return err
		}
		if !good {
			continue
		}

		err = rewriteImportsInFile(w.Path(), rw)
		if err != nil {
			fmt.Println("rewrite error: ", err)
			return err
		}
	}
	return nil
}
開發者ID:Kubuxu,項目名稱:gx-lua,代碼行數:35,代碼來源:rewrite.go

示例11: StdPopulate

func (r *Radio) StdPopulate() error {
	// Add a dummy manual playlist
	r.NewPlaylist("manual")

	// Add local directory
	playlistsDirs := []string{"/playlists"}
	playlistsDirs = append(playlistsDirs, path.Join(os.Getenv("HOME"), "playlists"))
	playlistsDirs = append(playlistsDirs, path.Join("/home", "playlists"))
	dir, err := os.Getwd()
	if err == nil && os.Getenv("NO_LOCAL_PLAYLISTS") != "1" {
		r.NewDirectoryPlaylist("local directory", dir)
		playlistsDirs = append(playlistsDirs, path.Join(dir, "playlists"))
	}

	// Add each folders in '/playlists' and './playlists'
	for _, playlistsDir := range playlistsDirs {
		walker := fs.Walk(playlistsDir)
		for walker.Step() {
			if walker.Path() == playlistsDir {
				continue
			}
			if err := walker.Err(); err != nil {
				logrus.Warnf("walker error: %v", err)
				continue
			}

			var realpath string
			if walker.Stat().IsDir() {
				realpath = walker.Path()
				walker.SkipDir()
			} else {
				realpath, err = filepath.EvalSymlinks(walker.Path())
				if err != nil {
					logrus.Warnf("filepath.EvalSymlinks error for %q: %v", walker.Path(), err)
					continue
				}
			}

			stat, err := os.Stat(realpath)
			if err != nil {
				logrus.Warnf("os.Stat error: %v", err)
				continue
			}
			if stat.IsDir() {
				r.NewDirectoryPlaylist(fmt.Sprintf("playlist: %s", walker.Stat().Name()), realpath)
			}
		}
	}

	// Add 'standard' music paths
	r.NewDirectoryPlaylist("iTunes Music", "~/Music/iTunes/iTunes Media/Music/")
	r.NewDirectoryPlaylist("iTunes Podcasts", "~/Music/iTunes/iTunes Media/Podcasts/")
	r.NewDirectoryPlaylist("iTunes Music", "/home/Music/iTunes/iTunes Media/Music/")
	r.NewDirectoryPlaylist("iTunes Podcasts", "/home/Music/iTunes/iTunes Media/Podcasts/")

	return nil
}
開發者ID:moul,項目名稱:radioman,代碼行數:57,代碼來源:radio.go

示例12: ExampleWalker

func ExampleWalker() {
	walker := fs.Walk("/usr/lib")
	for walker.Step() {
		if err := walker.Err(); err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
		fmt.Println(walker.Path())
	}
}
開發者ID:himanshugpt,項目名稱:evergreen,代碼行數:10,代碼來源:example_test.go

示例13: pythonSourceFiles

// Get all python source files under dir
func pythonSourceFiles(dir string, discoveredScripts map[string]bool) (files []string) {
	walker := fs.Walk(dir)
	for walker.Step() {
		if err := walker.Err(); err == nil && !walker.Stat().IsDir() && filepath.Ext(walker.Path()) == ".py" {
			file := walker.Path()
			_, found := discoveredScripts[file]

			if !found {
				files = append(files, filepath.ToSlash(file))
				discoveredScripts[file] = true
			}
		}
	}
	return
}
開發者ID:pombredanne,項目名稱:srclib-python,代碼行數:16,代碼來源:scan.go

示例14: rewriteTree

// rewriteTree recursively visits the go files in path, rewriting
// import statments according to the rules for func qualify.
func rewriteTree(path, qual string, paths []string) error {
	w := fs.Walk(path)
	for w.Step() {
		if w.Err() != nil {
			log.Println("rewrite:", w.Err())
			continue
		}
		if !w.Stat().IsDir() && strings.HasSuffix(w.Path(), ".go") {
			err := rewriteGoFile(w.Path(), qual, paths)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
開發者ID:hamfist,項目名稱:deppy,代碼行數:18,代碼來源:rewrite.go

示例15: AutoUpdate

func (p *Playlist) AutoUpdate() error {
	if p.Path == "" {
		logrus.Debugf("Playlist %q is not dynamic, skipping update", p.Name)
		return nil
	}

	// if we are here, the playlist is based on local file system
	logrus.Infof("Updating playlist %q", p.Name)

	p.Status = "updating"

	walker := fs.Walk(p.Path)

	for walker.Step() {
		if err := walker.Err(); err != nil {
			logrus.Warnf("walker error: %v", err)
			continue
		}
		stat := walker.Stat()

		if stat.IsDir() {
			switch stat.Name() {
			case ".git", "bower_components":
				walker.SkipDir()
			}
		} else {
			switch stat.Name() {
			case ".DS_Store":
				continue
			}

			p.NewLocalTrack(walker.Path())
		}
	}

	logrus.Infof("Playlist %q updated, %d tracks", p.Name, len(p.Tracks))
	if p.Stats.Tracks > 0 {
		p.Status = "ready"
	} else {
		p.Status = "empty"
	}
	p.ModificationDate = time.Now()

	return nil
}
開發者ID:moul,項目名稱:radioman,代碼行數:45,代碼來源:playlist.go


注:本文中的github.com/kr/fs.Walk函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。