192

In the Go tutorial, and most of the Go code I've looked at, packages are imported like this:

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)?

Eric Seidel
  • 2,781
  • 2
  • 17
  • 18

3 Answers3

257

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.

Import declaration          Local name of Sin

import   "lib/math"         math.Sin
import M "lib/math"         M.Sin
import . "lib/math"         Sin

Ref: http://golang.org/doc/go_spec.html#Import_declarations

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 56
    It should be noted that the Go team does not recommend using the dot import. It can cause some odd behaviour in certain cases and may not be in the language indefinitely. – jimt Jun 26 '11 at 05:16
  • 1
    It may be confusing in some scenarios, but I couldn't stand it in using go-linq. https://github.com/ahmetalpbalkan/go-linq – steviesama Jul 30 '16 at 00:27
  • 1
    Note that you still cannot get access to non-exported functions (non-uppercase functions). So this way of importing really sucks. – David 天宇 Wong Nov 22 '17 at 16:44
  • 1
    When compiling a program using dot import vs not using dot import. Is there a reason why there is huge difference if compiled file size? Using dot import compiles to bigger size. – majidarif Apr 10 '19 at 04:50
  • @majidarif: That's an interesting observation :) Could you please post a separate question for that with code that reproduces the issue? (I tried to do it myself, compiling a simple hello-world program with `import "fmt"` vs `import . "fmt"`, but in both cases it was the same size for me.) – Attilio May 07 '19 at 13:33
92

Here's an analogy for those coming from Python:

  • Go's import "os" is roughly equivalent to Python's import os
  • Go's import . "os" is roughly equivalent to Python's from os import *

In both languages, using the latter is generally frowned upon but there can be good reasons for doing it.

Evan Shaw
  • 23,839
  • 7
  • 70
  • 61
13

This should only be used in testing.

Here is some documentation in golang's wiki

If you've generated some mock code such as with mockgen and it imports your package code, and then your testing package also imports your package code, you get a circular dependency (Something golang chooses to let the user to decide how to resolve).

However, if inside your testing package you use dot notation on the package you're testing then they are treated as the same package and there is no circular dependency to be had!

Eli Davis
  • 415
  • 5
  • 6