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
+12.9k Python : Convert IPv6 address to decimal and back to IPv6
+28.8k Golang : Detect (OS) Operating System
+18.6k Golang : Find IP address from string
+5.4k Golang : Get S3 or CloudFront object or file information
+5.7k Golang : Struct field tags and what is their purpose?
+10.6k Golang : Simple File Server
+19.8k Golang : Append content to a file
+25.6k Golang : convert rune to integer value
+8.3k Golang : Implementing class(object-oriented programming style)
+13k Golang : Get terminal width and height example
+4.7k Adding Skype actions such as call and chat into web page examples
+14k Golang : concatenate(combine) strings