英文:
Go import modules "later"
问题
我们可以在Go语言中使用通用的模块导入吗?为了更清楚,这里有一个使用案例:
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "you requested", r.URL.Path)
}
func main() {
var moduleName string = "/path/to/module"
// 这里是你尝试导入 moduleName 的地方,但是会报错。
http.HandleFunc("/", handler)
http.ListenAndServe(":8000", nil)
}
在上面的代码中,你可以看到我尝试导入 moduleName,但是这会导致错误。是否有一些解决方法?
英文:
Can we have generic importing of modules with go. To be more clear, here is use case:
package main
import (
"fmt"
"net/http"
)
json handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "you requested", r.URL.Path)
}
func main() {
var moduleName String = "/path/to/module"
import moduleName
http.HandleFunc("/", handler)
http.ListenAndServe(":8000", nil)
}
So in main you can see that I am trying to import moduleName, but this gives me an error.
Is there some workaround for this?
答案1
得分: 8
Go是一种静态编译语言,不像Python那样是一种解释语言。在Go中,导入操作发生在编译时而不是运行时。所以简而言之,不,你只能在包级别上导入内容。
官方定义明确说明了这一点:
导入声明表示包含该声明的源文件依赖于导入包的功能,并且使得可以访问该包的导出标识符。
关于导入还有一个有趣的注意事项,如果导入的包中有一个init()
函数,该函数将在程序启动时被调用以初始化该包;来自文档的说明:
如果一个包有导入,那么在初始化该包本身之前,导入的包将被初始化。
这为动态初始化留下了一些空间,但与动态导入相去甚远。
英文:
Go is a statically compiled language, not an interpreted language like Python. Your imports happen in compile time, not in run time. So in short, no, you can only import stuff on the package level.
The official definition makes this clear:
> An import declaration states that the source file containing the declaration depends on functionality of the imported package and enables access to exported identifiers of that package.
One more interesting note on imports is that if a package is imported and has an init()
function, this function will be called to initialize the package, on program startup; from the docs:
> If a package has imports, the imported packages are initialized before initializing the package itself
This leaves some room for dynamic intiailization, but it's far from dynamic imports.
答案2
得分: 1
Google Go不原生支持动态导入。关于这个主题的更多信息可以在这里阅读:https://stackoverflow.com/questions/8076034/how-to-import-package-by-path-from-string-in-go
在一个论坛讨论中,有人建议使用特定模块调用编译器来解决这个问题。然而,这并不是通常的做法。可以在这里找到该讨论:https://groups.google.com/forum/#!topic/golang-nuts/Rm0gcBPbxLk
从一般意义上讲,我建议不要使用这样的方案。可能有不同的方法来实现具有相同功能的程序。如果在Google Go中找不到其他方案,请尝试在C++中搜索相同类型的方案,它们通常非常相似。
英文:
Google Go doesn't natively support dynamic imports. More on this subject can be read here: https://stackoverflow.com/questions/8076034/how-to-import-package-by-path-from-string-in-go
In a forum discussion a solution that is suggested is calling the compiler with your specific module. This is however not general practice. The discussion can be found here: https://groups.google.com/forum/#!topic/golang-nuts/Rm0gcBPbxLk
In a general sense I would advise against any such schemes. There are probably different ways to implement the program with the same functionality. If you can't find another scheme for Google Go, try searching for the same kind of schemes in C++, they are usually quite similar.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论