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
+13.1k Golang : Handle or parse date string with Z suffix(RFC3339) example
+5.4k Golang : What is StructTag and how to get StructTag's value?
+8k Golang : Handle Palindrome string with case sensitivity and unicode
+7.3k Golang : How to fix html/template : "somefile" is undefined error?
+14.2k Golang : Fix image: unknown format error
+18.7k Unmarshal/Load CSV record into struct in Go
+14.7k Golang : Reset buffer example
+5.4k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+13.6k Golang : Strings comparison
+6.4k Golang : Break string into a slice of characters example
+12.1k Golang : Pagination with go-paginator configuration example
+10.9k Golang : How to transmit update file to client by HTTP request example