patterngoCritical
What does the '.' (dot or period) in a Go import statement do?
Viewed 0 times
periodstatementthedoeswhatdotimport
Problem
In the Go tutorial, and most of the Go code I've looked at, packages are imported like this:
But in http://bazaar.launchpad.net/~niemeyer/lpad/trunk/view/head:/session_test.go, the gocheck package is imported with a
What is the significance of the
import (
"fmt"
"os"
"launchpad.net/lpad"
...
)But in http://bazaar.launchpad.net/~niemeyer/lpad/trunk/view/head:/session_test.go, the gocheck package is imported with a
. (period):import (
"http"
. "launchpad.net/gocheck"
"launchpad.net/lpad"
"os"
)What is the significance of the
. (period)?Solution
It allows the identifiers in the imported package to be referred to in the local file block without a qualifier.
If an explicit period (.) appears instead of a name, all the package's exported identifiers will be declared in the current file's file block and can be accessed without a qualifier.
Assume we have compiled a package containing the package clause package math, which exports function Sin, and installed the compiled package in the file identified by "lib/math". This table illustrates how Sin may be accessed in files that import the package after the various types of import declaration.
Ref: http://golang.org/doc/go_spec.html#Import_declarations
If an explicit period (.) appears instead of a name, all the package's exported identifiers will be declared in the current file's file block and can be accessed without a qualifier.
Assume we have compiled a package containing the package clause package math, which exports function Sin, and installed the compiled package in the file identified by "lib/math". This table illustrates how Sin may be accessed in files that import the package after the various types of import declaration.
Import declaration Local name of Sin
import "lib/math" math.Sin
import M "lib/math" M.Sin
import . "lib/math" SinRef: http://golang.org/doc/go_spec.html#Import_declarations
Code Snippets
Import declaration Local name of Sin
import "lib/math" math.Sin
import M "lib/math" M.Sin
import . "lib/math" SinContext
Stack Overflow Q#6478962, score: 276
Revisions (0)
No revisions yet.