‘.’ (点号或句号) 在Go的导入语句中有什么作用?

huangapple go评论88阅读模式
英文:

What does the '.' (dot or period) in a Go import statement do?

问题

在Go教程中,以及我查看的大部分Go代码中,包是这样导入的:

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

但是在http://bazaar.launchpad.net/~niemeyer/lpad/trunk/view/head:/session_test.go中,gocheck包是用.(句点)导入的:

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

.(句点)的意义是什么?

英文:

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

答案1

得分: 255

它允许在本地文件块中引用导入包中的标识符,而无需限定符。

> 如果出现一个显式的句点(.)而不是一个名称,所有包的导出标识符将在当前文件的文件块中声明,并且可以在没有限定符的情况下访问。
>
> 假设我们已经编译了一个包,其中包含包子句package math,该包导出函数Sin,并且已将编译的包安装在由“lib/math”标识的文件中。此表说明了在导入包后如何在导入包的文件中访问Sin的各种类型的导入声明。

导入声明          Sin的本地名称

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

参考:http://golang.org/doc/go_spec.html#Import_declarations

英文:

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

答案2

得分: 92

这是一个给那些从Python过来的人的类比:

  • Go的import "os"大致相当于Python的import os
  • Go的import . "os"大致相当于Python的from os import *

在两种语言中,使用后者通常是不被赞同的,但有时也有好的理由这样做。

英文:

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.

答案3

得分: 13

这应该只在测试中使用。

这里是一些关于golang的维基文档

如果你生成了一些模拟代码,比如使用mockgen,并且它导入了你的包代码,然后你的测试包导入了你的包代码,你就会得到一个循环依赖(这是golang选择让用户决定如何解决的问题)。

然而,如果在你的测试包中使用点符号表示你正在测试的包,那么它们将被视为同一个包,就不会出现循环依赖的问题!

英文:

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!

huangapple
  • 本文由 发表于 2011年6月26日 00:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/6478962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定