Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import everything from a package

Tags:

package

go

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")
}
like image 282
Pika Supports Ukraine Avatar asked Oct 13 '18 19:10

Pika Supports Ukraine


People also ask

How import all modules from package?

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.

How do I import everything into Python?

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.

How do I import all functions?

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.

What is __ all __ in Python?

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.


1 Answers

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
like image 164
peterSO Avatar answered Sep 25 '22 22:09

peterSO