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
+10.9k Golang : How to transmit update file to client by HTTP request example
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+7.9k Setting $GOPATH environment variable for Unix/Linux and Windows
+16.6k Golang : Delete files by extension
+7.2k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+9.1k Golang : Intercept and compare HTTP response code example
+8.6k Golang : Set or add headers for many or different handlers
+14.5k How to automatically restart your crashed Golang server
+14k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+5.9k Golang : Extract unicode string from another unicode string example
+9.4k Golang : Timeout example
+17.1k Golang : Capture stdout of a child process and act according to the result