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
+35.5k Golang : Smarter Error Handling with strings.Contains()
+7.9k Setting $GOPATH environment variable for Unix/Linux and Windows
+6.4k Golang : Break string into a slice of characters example
+13.3k Golang : Linear algebra and matrix calculation example
+12k Golang : Clean formatting/indenting or pretty print JSON result
+7.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+9.3k Golang : Generate random Chinese, Japanese, Korean and other runes
+4.7k JavaScript: Add marker function on Google Map
+21.2k Golang : How to force compile or remove object files first before rebuild?
+6.6k Golang : Totalize or add-up an array or slice example
+8.8k Golang : Gorilla web tool kit schema example