简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM