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
+25.9k Golang : How to read integer value from standard input ?
+8.8k Golang : Accept any number of function arguments with three dots(...)
+16.4k Golang : Test floating point numbers not-a-number and infinite example
+5.8k Cash Flow : 50 days to pay your credit card debt
+20k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+8.8k Golang : Executing and evaluating nested loop in html template
+16.8k Golang : Get own process identifier
+6.9k Fix sudo yum hang problem with no output or error messages
+17k Golang : Get number of CPU cores
+22.5k Golang : Convert Unix timestamp to UTC timestamp
+24.1k Golang : Upload to S3 with official aws-sdk-go package
+19.4k Golang : How to count the number of repeated characters in a string?