How to Import All Exported Identifiers from a Go Package?

Linda Hamilton
Release: 2024-10-26 12:02:03
Original
798 people have browsed it

How to Import All Exported Identifiers from a Go Package?

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>
Copy after login

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>
Copy after login

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:

  • package main defines the main package.
  • import . "fmt" imports the full contents of the fmt package, allowing direct access to its exported identifiers.
  • func main() defines the entry point for the program.
  • Println("Hello, world") calls the exported Println function from the fmt package to print the specified message.

This modified code will compile successfully and produce the same output:

Hello, world
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!