2016-11-11 85 views
-1

我在走的很远对于新手,我试图建立这种一般方面的功能:数组数组中特定位置的类型?

mapOfResults = ThingDoer([ 
    ["One", int, -1, true], 
    ["Flying", string, "", true], 
    ["Banana", bool, false, true] 
]) 

但我甚至无法计算其签名(签名是在去为它甚至适当的期限?所有参数的定义等)。

我说的是这个结构:

func ThingDoer(config ThisIsWhatICannotFigure) map[string]Results { 
    // the body of my function 
} 

如何定义的类型这样的参数?

回答

2

试试这个:

type ConfigItem struct { 
    Name string 
    Value interface{} 
    SomethingElse bool 
} 

mapOfResults = ThingDoer([]ConfigItem{ 
    {"One", -1, true}, 
    {"Flying", "", true}, 
    {"Banana", false, true}, 
}) 

的ThingDoer可以使用type switch确定值类型:

func ThingDoer(config []ConfigItem) map[foo]bar { 
    for _, item := range config { 
     switch v := item.Value.(type) { 
     case int: 
     // v is int 
     case bool: 
     // v is bool 
     case string: 
     // v is string 
     } 
    } 
} 

playground example

+0

对我的作品!显然,我必须等一下才能接受你的回答。谢谢! –