变量可以用作函数调用的占位符吗?

我正在编写一个程序,它有几个结构和函数来以不同的方式处理这些结构。我有一个通用函数,它根据输入调用所需的函数。是否有通用方法来使用getStruct()返回的值?


package main


var X func(s []string) A

var Y func(s []string) B


type A struct {

    Name  string

    Place string

}


type B struct {

    Name  string

    Place string

    Value string

}


func newA(s []string) A {

    a := A{

        Name:  s[0],

        Place: s[1],

    }

    return a

}


func newB(s []string) B {

    a := B{

        Name:  s[0],

        Place: s[1],

        Value: s[2],

    }

    return a

}


func getStruct(t string) interface{} {


    switch {

    case t == "A":

        return X

    case t == "B":

        return Y

    default:

        return //someStruct

    }


}


func main() {


    buildNewStruct := getStruct("A") //Lets assume "A" here is got as an argument


    var strSlice = []string{"Bob", "US"}

    buildNewStruct(strSlice) //How to do this operation? 

                             //I am hoping to use buildNewStruct(strSlice) to dynamically call 

                             //either of newA(strSlice) or newB(strSlice) function


}


由于我是新手,我不确定这样的事情是否可能。


三国纷争
浏览 93回答 2
2回答

慕慕森

您可以使用该reflect包将结构属性设置为[]interface{}切片中的等效索引定位值。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "reflect")func main() {&nbsp; &nbsp; var a A&nbsp; &nbsp; err := decode(&a, []interface{}{"Name", "Place"})&nbsp; &nbsp; log.Println(err)&nbsp; &nbsp; log.Println(a)}func decode(dst interface{}, values []interface{}) error {&nbsp; &nbsp; rvptr := reflect.ValueOf(dst)&nbsp; &nbsp; if rvptr.Kind() != reflect.Ptr {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("value must be ptr")&nbsp; &nbsp; }&nbsp; &nbsp; rv := rvptr.Elem()&nbsp; &nbsp; if rv.NumField() < len(values) {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("too many values")&nbsp; &nbsp; }&nbsp; &nbsp; if rv.NumField() > len(values) {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Errorf("too few values")&nbsp; &nbsp; }&nbsp; &nbsp; rvalues := reflect.ValueOf(values)&nbsp; &nbsp; for i := range values {&nbsp; &nbsp; &nbsp; &nbsp; f := rv.FieldByIndex([]int{i})&nbsp; &nbsp; &nbsp; &nbsp; f.Set(rvalues.Index(i).Elem())&nbsp; &nbsp; }&nbsp; &nbsp; return nil}type A struct {&nbsp; &nbsp; Name&nbsp; string&nbsp; &nbsp; Place string}type B struct {&nbsp; &nbsp; Name&nbsp; string&nbsp; &nbsp; Place string&nbsp; &nbsp; Value string}印刷$ go run main.go&nbsp;2019/11/21 17:00:17 <nil>2019/11/21 17:00:17 {Name Place}

肥皂起泡泡

问题在于函数的返回类型。func newA(in []string) interface{} {...}func newB(in []string) interface{} {...}func getStruct(name string) func([]string) interface{} {&nbsp; switch name {&nbsp; &nbsp; &nbsp;case "A": return newA&nbsp; &nbsp; &nbsp;case "B": return newB&nbsp; }&nbsp; return nil}func main() {&nbsp; buildNewStruct := getStruct("A")&nbsp;&nbsp; var strSlice = []string{"Bob", "US"}&nbsp; str:=buildNewStruct(strSlice)&nbsp; if a, ok:=str.(A); ok {&nbsp; &nbsp; &nbsp;...&nbsp; }}使用这种方法,即使您通过调用统一节省了一些代码buildNewStruct(),您也必须使用类型断言来确定该函数返回的内容,因此这可能没有多大意义。但这取决于您的具体用例。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go