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")
}
with this:
import "fmt"
func main() {
Println("Hello, world")
}
Importing module from a package We can import modules from packages using the dot (.) operator. Now, if this module contains a function named select_difficulty() , we must use the full name to reference it. Now we can directly call this function.
So you will need to create a list of strings of everything in your package and then do a "from packageName import *" to import everything in this module so when you import this elsewhere, all those are also imported within this namespace.
We can use import * if we want to import everything from a file in our code. We have a file named functions.py that contains two functions square() and cube() . We can write from functions import * to import both functions in our code. We can then use both square() and cube() functions in our code.
In the __init__.py file of a package __all__ is a list of strings with the names of public modules or other objects. Those features are available to wildcard imports. As with modules, __all__ customizes the * when wildcard-importing from the package.
The Go Programming Language Specification
Import declarations
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")
}
Playground: https://play.golang.org/p/xl7DIxxMlU5
Output:
Hello, world
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With