从包中导入所有内容 [英] Import everything from a package

查看:84
本文介绍了从包中导入所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以导入包的全部内容,从而不必在调用时以包名称为包中的内容添加前缀?

I'm wondering if there is any way to import the full contents of a package so that I don't have to prefix calls to things in the package with a package name?

例如,有一种替代方法:

For example, is there a way to replace this:

import "fmt"
func main() {
    fmt.Println("Hello, world")
}

与此:

import "fmt"
func main() {
    Println("Hello, world")
}

推荐答案

Go编程语言规范

导入声明

如果出现一个明确的句点(.)而不是名称,则表示该软件包的所有 在该包的包块中声明的导出的标识符将是 在导入源文件的文件块中声明,并且必须是 没有限定符即可访问.

If an explicit period (.) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block and must be accessed without a qualifier.


例如,


For example,

package main

import . "fmt"

func main() {
    Println("Hello, world")
}

游乐场: https://play.golang.org/p/xl7DIxxMlU5

输出:

Hello, world

这篇关于从包中导入所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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