Golang go/build.Context.SrcDirs() function examples
package go/build
SrcDirs returns a list of package source root directories. It draws from the current Go root and Go path but omits directories that do not exist.
Golang go/build.Context.SrcDirs() function usage examples
Example 1:
// Gets all local Go packages (from GOROOT and all GOPATH workspaces).
func GetGoPackages(out chan<- *GoPackage) {
for _, root := range build.Default.SrcDirs() {
_ = filepath.Walk(root, func(path string, fi os.FileInfo, _ error) error {
switch {
case !fi.IsDir():
return nil
case path == root:
return nil
case strings.HasPrefix(fi.Name(), "."):
return filepath.SkipDir
default:
importPath, err := filepath.Rel(root, path)
if err != nil {
return nil
}
if goPackage := GoPackageFromImportPath(importPath); goPackage != nil {
out <- goPackage
}
return nil
}
})
}
close(out)
}
Example 2:
func init() {
trimPaths = build.Default.SrcDirs()
}
References :
https://github.com/shurcooL/go/blob/master/gists/gist8018045/main.go
Advertisement
Something interesting
Tutorials
+8.8k Golang : Get final balance from bit coin address example
+13.9k Golang : Get current time
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+13.1k Golang : Handle or parse date string with Z suffix(RFC3339) example
+8.4k Golang : Ackermann function example
+16k Golang : Generate universally unique identifier(UUID) example
+23.9k Golang : Use regular expression to validate domain name
+36.4k Golang : Convert date or time stamp from string to time.Time type
+28.6k Golang : Read, Write(Create) and Delete Cookie example
+6.1k Golang : How to write backslash in string?
+22.1k Golang : Match strings by wildcard patterns with filepath.Match() function
+25k Golang : Create PDF file from HTML file