简体   繁体   English

如何导出名称以使其可全局访问?

[英]How to export a name so that it becomes globally accessible?

I want to export a function from my package so that I can use it without typing a package name before it, how to do that? 我想从我的包中导出一个函数,这样我可以在不键入包名的情况下使用它,该怎么做?

import "mypackage"

func main() {
   mypackage.myfunc()    <-- that's what I have already
   myfunc()              <-- that's what I need
}

You can use one of the followings: 您可以使用以下之一:

import (  
    .     "mypackage"                           // without a name  
    mp    "my/other/package"                    // rename
    _     "my/totally/diffrent/package"         // import a package solely for its side-effects (initialization)
)

Obviously, this pattern is not recommended since it can cause name conflicts with other packages. 显然,不建议使用此模式,因为它可能导致与其他软件包的名称冲突。

Check out the dot imports bulletin 查看点导入公告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何全局设置cookiejar,以便在每个http.Client请求中都存在cookie - How to set cookiejar globally so that the cookies are present in every http.Client request 如何全局访问处理程序值 - How to access handler value globally 如何全局定义结构并重用其包 - How to define a struct globally and reuse it packages 当Golang的元帅转换为json时,数组部分变成了object,所以我想保留数组 - When converted to json by Golang's Marshal, the array part becomes an object, so I want to keep the array 导出 java ca 证书,以便它可以从 go 中使用 - Export java ca certificate so that it is usable from go 如何安装特定版本的 golint 以供全球使用? - How can I install a specific version of golint for use globally? 如何在GoLang上的不同处理程序上全局共享MySQL驱动程序 - How to share globally the MySQL driver on different handlers on GoLang 如何在Golang中导出为CSV? - How to export to CSV in Golang? AWS ELB返回https:// domain_name:8000的404,但可通过public_ip:8000访问 - AWS ELB return 404 for https://domain_name:8000 but accessible via public_ip:8000 如何在golang的所有包中访问全局常量? - How to have a global constant accessible in all packages in golang?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM