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
+17.5k Golang : Find smallest number in array
+12.4k Golang : Extract part of string with regular expression
+6.9k Fix sudo yum hang problem with no output or error messages
+4.8k PHP : Extract part of a string starting from the middle
+20.2k Golang : How to get struct tag and use field name to retrieve data?
+7.9k Golang : Ways to recover memory during run time.
+36.5k Golang : Validate IP address
+8.1k Golang : Get all countries phone codes
+22.4k Golang : Read directory content with filepath.Walk()
+9.4k Golang : Qt Yes No and Quit message box example
+19.4k Golang : How to count the number of repeated characters in a string?
+9.4k Golang : Timeout example