[英]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. 显然,不建议使用此模式,因为它可能导致与其他软件包的名称冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.