Golang go/build.Context.Import() function examples
package go/build
Import returns details about the Go package named by the import path, interpreting local import paths relative to the srcDir directory. If the path is a local import path naming a package that can be imported using a standard import path, the returned package will set p.ImportPath to that path.
In the directory containing the package, .go, .c, .h, and .s files are considered part of the package except for:
.go files in package documentation
files starting with _ or . (likely editor temporary files)
files with build constraints not satisfied by the context
If an error occurs, Import returns a non-nil error and a non-nil *Package containing partial information.
Golang go/build.Context.Import() function usage examples
Example 1:
pkg, err = build.Default.Import(name, ".", build.ImportMode(1))
Example 2:
func packageWithName(name string) (pkg *build.Package, err error) {
pkg, err = build.Default.Import(name, ".", build.ImportMode(0))
if err == nil {
return
}
pkg, err = build.Default.Import(name, ".", build.ImportMode(1))
return
}
References :
https://github.com/onsi/ginkgo/blob/master/ginkgo/convert/package_rewriter.go
Advertisement
Something interesting
Tutorials
+19.5k Golang : Example for DSA(Digital Signature Algorithm) package functions
+9.5k Golang : Extract or copy items from map based on value
+15.2k Golang : How to add color to string?
+9.6k Golang : How to generate Code 39 barcode?
+12.1k Golang : Sort and reverse sort a slice of runes
+24.5k Golang : GORM read from database example
+9.7k Golang : Populate slice with sequential integers example
+4.7k JavaScript: Add marker function on Google Map
+3.4k Golang : Fix go-cron set time not working issue
+29.9k Golang : Get and Set User-Agent examples
+12.5k Golang : Arithmetic operation with numerical slices or arrays example
+9.9k Golang : Translate language with language package example