什么是'。' (点或周期)在Go导入语句中做? [英] What does the '.' (dot or period) in a Go import statement do?

查看:155
本文介绍了什么是'。' (点或周期)在Go导入语句中做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Go教程和我看到的大部分Go代码中,软件包都是这样导入的:

 进口(
fmt
os
launchpad.net/lpad
...

但在 http://bazaar.launchpad.net/~niemeyer/lpad/trunk/view/head:/session_test.go ,gocheck软件包会导入一个

 导入(
http
。launchpad.net/ gocheck
launchpad.net/lpad
os

(句点)的意义何在?

解决方案




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




 导入声明本地名称Sin 

导入lib / mathmath.Sin
导入Mlib / mathM.Sin
导入。 lib / mathSin

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


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

解决方案

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

这篇关于什么是'。' (点或周期)在Go导入语句中做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆