英文:
How to assign a package to a variable in Go?
问题
首先,问题中的词“variable”可能是错误的,但我假设详细的问题解释了为什么我使用了“variable”这个词。
我有两个具有不同名称但具有完全相同函数的包。
根据用户的输入,我想使用一个包的函数。但是,我不想切换包,而是想将包分配给一个具有通用名称的新函数,如何做到这一点?如果不可能,为什么?
// foo/bar.go
package foo
func Test() {
fmt.Println("hola from bar")
}
---
// baz/bar.go
package baz
func Test() {
fmt.Println("hola from baz")
}
---
// main.go
package main
import (
"foo"
"baz"
)
func main() {
thePackage := flag.String("package", "foo", "a package")
if thePackage == "foo" {
howToSetThis := foo // 如何做到这一点?
} else {
howToSetThis := baz // 如何做到这一点?
}
howToSetThis.Test() // 执行两个 bar.go 文件中的一个的 Println
}
在JavaScript中,我会这样做(简化):
function foo(){
console.log("hola from foo")
}
function baz(){
console.log("hola from baz")
}
if(somethingInput == "foo"){
var howToSetThis = foo;
} else {
var howToSetThis = baz;
}
howToSetThis();
英文:
First of all, the word "variable" might be wrong in the question but I assume the detailed question explains why I'm using the word "variable".
I have two packages with different names but the exact same function(s).
Depending on the input of the user I want to use the function of a package. But instead of switching over the packages I would like to assign the package to a new function with a generic name, how to do this? And if it's not possible, why?
// foo/bar.go
package foo
func Test() {
fmt.Println("hola from bar")
}
// baz/bar.go
package baz
func Test() {
fmt.Println("hola from baz")
}
// main.go
package main
import (
"foo"
"baz"
)
func main() {
thePackage := flag.String("package", "foo", "a package")
if thePackage == "foo" {
howToSetThis := foo // How to do this?
} else {
howToSetThis := baz // How to do this?
}
howToSetThis.Test() // execs Println from one of the two bar.go files
}
In JavaScript I would do something like (simplified):
function foo(){
console.log("hola from foo")
}
function baz(){
console.log("hola from baz")
}
if(somethingInput == "foo"){
var howToSetThis = foo;
} else {
var howToSetThis = baz;
}
howToSetThis();
答案1
得分: 3
你可以在一个包中定义一个接口,该接口需要你想要在这两个包中拥有的函数。
package intfc
type Intfc interface {
Test()
}
然后,在不同的包中定义两个实现该接口的结构体。
package foo
import "fmt"
type Foo struct {
}
func (f Foo) Test() {
fmt.Println("hola from foo")
}
package baz
import "fmt"
type Baz struct {
}
func (f Baz) Test() {
fmt.Println("hola from baz")
}
然后,在主函数中进行如下操作:
package main
import (
"foo"
"baz"
"intfc"
"flag"
)
func main() {
var howToSetThis intfc.Intfc
thePackage := flag.String("package", "foo", "a package")
if *thePackage == "foo" {
howToSetThis = foo.Foo{}
} else {
howToSetThis = baz.Baz{}
}
howToSetThis.Test() // 执行来自两个 bar.go 文件中的 Println
}
英文:
You could define a interface in a package that requires the functions you want to have in those two packages .
package intfc
type Intfc interface {
Test()
}
And then two structs in different packages which implements that interface
package foo
import "fmt"
type Foo struct {
}
func (f Foo) Test() {
fmt.Println("hola from foo")
}
and
package baz
import "fmt"
type Baz struct {
}
func (f Baz) Test() {
fmt.Println("hola from baz")
}
And then in main do something like this:
package main
import (
"foo"
"baz"
"intfc"
"flag"
)
func main() {
var howToSetThis intfc.Intfc
thePackage := flag.String("package", "foo", "a package")
if *thePackage == "foo" {
howToSetThis = foo.Foo{}
} else {
howToSetThis = baz.Baz{}
}
howToSetThis.Test() // execs Println from one of the two bar.go files
}
答案2
得分: 0
在编译时,import
指令几乎是第一个被解析的指令。这意味着在导入指令中出现的包名将被用作解析其他所有内容的参考。
因此,你不能像在JavaScript中那样使用动态语言结构。Cerise的回答是正确的。
英文:
At compile time import
is practically if not the first directive to get parsed. That means whatever package name is present in the import directive will be the one used as reference in parsing everything else.
Hence you can't use a dynamic language structure like what you did in Javascript. Cerise's answer is correct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论