装入包装内容,而后不使用其名称

装入包装内容,而后不使用其名称

问题描述:

Is there a way to load the contents of a package in go without needing to use the package name? For example, in Python you can do:

from somepackage import *
# access function from somepackage foo
foo()

I would like to do that in Go. I tried:

import _ "path/to/my/package"

but it didn't work. I'm having trouble articulating myself to find the solution online, if there is one.

是否可以在不使用软件包名称的情况下在go中加载软件包的内容? 例如,在Python中,您可以执行以下操作: p>

  from somepackage import * 
#访问某些软件包的函数foo 
foo()
  code>  pre> \  n 
 

我想在Go中做到这一点。 我试过了: p>

  import _“ path / to / my / package” 
  code>  pre> 
 
 

,但是没有 工作。 如果有解决方案,我很难说明自己在网上找到解决方案。 p> div>

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.

Use a period (.) instead of a name. For example,

package main

import (
    "fmt"
    . "time"
)

func main() {
    fmt.Println(Now()) // time.Now()
}

Output:

2009-11-10 23:00:00 +0000 UTC