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
+6.7k Golang : Experimental emojis or emoticons icons programming language
+36.5k Golang : Save image to PNG, JPEG or GIF format.
+10.8k Golang : Command line file upload program to server example
+9.3k Golang : Generate random Chinese, Japanese, Korean and other runes
+16.3k Golang : Loop each day of the current month example
+14.9k Golang : How to check for empty array string or string?
+9k Golang : How to use Gorilla webtoolkit context package properly
+17.7k How to enable MariaDB/MySQL logs ?
+7.5k Golang : Get YouTube playlist
+15.2k Golang : Accurate and reliable decimal calculations
+8.6k Android Studio : Import third-party library or package into Gradle Scripts
+23.9k Golang : Fix type interface{} has no field or no methods and type assertions example