gpt4 book ai didi

go - 如何导出名称以便全局访问?

转载 作者:IT王子 更新时间:2023-10-29 02:29:23 25 4
gpt4 key购买 nike

我想从我的包中导出一个函数,这样我就可以在不输入包名的情况下使用它,该怎么做?

import "mypackage"

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

最佳答案

您可以使用以下之一:

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

显然,不推荐使用这种模式,因为它会导致与其他包的名称冲突。

Check out the dot imports bulletin

关于go - 如何导出名称以便全局访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42473739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com