Golang go/token.FileSet.Iterate function examples

package go/token

Iterate calls f (inner function) for the files in the file set in the order they were added until f returns false.

Golang go/token.FileSet.Iterate function usage examples

Example 1:

 func printStats(d time.Duration) {
  fileCount := 0
  lineCount := 0
  fset.Iterate(func(f *token.File) bool {
 fileCount++
 lineCount += f.LineCount()
 return true
  })
  fmt.Printf(
 "%s (%d files, %d lines, %d lines/s)\n",
 d, fileCount, lineCount, int64(float64(lineCount)/d.Seconds()),
  )
 }

Example 2:

 innerfunc := func(f *token.File) bool {
 files = append(files, f.Name())
 return true
 }

 func scopeFiles(prog *loader.Program) []string {
  files := make([]string, 0)
  prog.Fset.Iterate(innerfunc) // <-- here
  sort.Strings(files)
  return files
 }

References :

https://github.com/fzipp/pythia/blob/master/main.go

http://golang.org/pkg/go/token/#FileSet.Iterate

Advertisement