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
+17.8k Golang : Iterate linked list example
+10k Golang : Convert octal value to string to deal with leading zero problem
+19.9k Golang : Count JSON objects and convert to slice/array
+8.2k Golang : Add build version and other information in executables
+8.3k Golang : Configure Apache and NGINX to access your Go service example
+9k Golang : automatically figure out array length(size) with three dots
+11.6k Android Studio : Create custom icons for your application example
+9.9k Golang : Turn string or text file into slice example
+16.1k Golang : How to check if input from os.Args is integer?
+3.6k Java : Get FX sentiment from website example
+14.4k Golang : On enumeration
+5.4k Javascript : How to loop over and parse JSON data?