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
+16.8k Golang : Get own process identifier
+9.2k Golang : Write multiple lines or divide string into multiple lines
+14k Golang : Reverse IP address for reverse DNS lookup example
+11.2k Google Maps URL parameters configuration
+26k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+9.9k Golang : Translate language with language package example
+5.6k Golang : Shortening import identifier
+15.7k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+13.6k Golang : Qt progress dialog example
+8.8k Yum Error: no such table: packages
+10.2k Golang : Check a web page existence with HEAD request example
+6.3k Golang : Detect face in uploaded photo like GPlus