Home >Backend Development >Golang >What Does the '.' (Dot) Mean in Go's Import Statements?

What Does the '.' (Dot) Mean in Go's Import Statements?

Susan Sarandon
Susan SarandonOriginal
2024-12-21 10:04:11387browse

What Does the

Understanding the "." (Dot) in Go Import Statements

In Go, the dot (.) symbol in an import statement holds a specific significance. While most packages are typically imported using the format shown below:

import (
    "fmt"
    "os"
    "launchpad.net/lpad"
    ...
)

There are instances where a package is imported with a dot, as illustrated in this example:

import (
    "http"
    . "launchpad.net/gocheck"
    "launchpad.net/lpad"
    "os"    
)

Purpose of the Dot (.)

The dot (.) symbol in the import statement allows all exported identifiers from the imported package to be accessible in the current file block without the need for a qualifier. This means that identifiers from the imported package can be directly accessed by name instead of using the package name as a prefix.

Example

Consider the following package clause:

package math

Which exports the Sin function. After compiling and installing the package in a file named "lib/math," the following table demonstrates how the Sin function can be accessed depending on the import declaration used:

Import Declaration Local Name of Sin
import "lib/math" math.Sin
import M "lib/math" M.Sin
import . "lib/math" Sin

As you can see, using the dot (.) allows for direct access to the identifier Sin without the need for a qualifier.

Reference

This behavior is documented in the Go specification at:

https://golang.org/doc/go_spec.html#Import_declarations

The above is the detailed content of What Does the '.' (Dot) Mean in Go's Import Statements?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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