Importing the Full Contents of a Package
As mentioned in the Go Programming Language Specification, importing the full contents of a package is possible by using an explicit period (.) instead of a name in an import declaration. This imports all the exported identifiers declared in the package's package block, allowing you to access them without a qualifier.
For instance, consider the following code:
<code class="go">import "fmt" func main() { fmt.Println("Hello, world") }</code>
In this example, you must prefix calls to fmt with the package name. To import the full contents of the fmt package, you can use the following code:
<code class="go">import . "fmt" func main() { Println("Hello, world") }</code>
By importing the full contents of the package, you can access the exported identifiers directly, eliminating the need for the package name prefix.
Here's a breakdown of the revised code:
This modified code will compile successfully and produce the same output:
Hello, world
For additional clarity, here's an example from the Go Playground: https://play.golang.org/p/xl7DIxxMlU5
The above is the detailed content of How to Import All Exported Identifiers from a Go Package?. For more information, please follow other related articles on the PHP Chinese website!