Golang go/parser.ParseDir() function example
package go/parser
ParseDir calls ParseFile for all files with names ending in ".go" in the directory specified by path and returns a map of package name -> package AST with all the packages found.
If filter != nil, only the files with os.FileInfo entries passing through the filter (and ending in ".go") are considered. The mode bits are passed to ParseFile unchanged. Position information is recorded in fset.
If the directory couldn't be read, a nil map and the respective error are returned. If a parse error occurred, a non-nil but incomplete map and the first error encountered are returned.
Golang go/parser.ParseDir() function usage example
var pkgRealpath, pkgpath string
fileSet := token.NewFileSet()
astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
name := info.Name()
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}, parser.ParseComments)
References :
Advertisement
Something interesting
Tutorials
+13.4k Golang : error parsing regexp: invalid or unsupported Perl syntax
+13k Golang : Calculate elapsed years or months since a date
+19.7k Golang : Archive directory with tar and gzip
+40.1k Golang : UDP client server read write example
+11.6k Swift : Convert (cast) Float to String
+25.3k Golang : Get current file path of a file or executable
+5.2k Golang : Print instead of building pyramids
+16k Golang : Get sub string example
+10.1k Golang : Test a slice of integers for odd and even numbers
+5.8k Golang : Markov chains to predict probability of next state example
+9.4k Golang : Timeout example