interface{}任意の型を受け入れる型です。
conf := map[string] interface{} {
"name": "Default",
"server": "localhost",
"timeout": 120,
}
conf["name"]は ではinterface{}なく 、stringは でconf["timeout"]はありinterface{}ませんint。likeconf["name"]を取る関数に渡すことはできますが、 like を取る関数にinterface{}渡すことはfmt.Printlnできません.stringstrings.ToUpperinterface{}string
name := conf["name"].(string)
fmt.Println("name:", strings.ToUpper(name))
server := conf["server"].(string)
fmt.Println("server:", strings.ToUpper(server))
timeout := conf["timeout"].(int)
fmt.Println("timeout in minutes:", timeout / 60)
あなたの問題に合うかもしれない別の解決策は、構造体を定義することです:
type Config struct {
Name string
Server string
Timeout int
}
構成を作成します。
conf := Config{
Name: "Default",
Server: "localhost",
Tiemout: 60,
}
アクセス構成:
fmt.Println("name:", strings.ToUpper(conf.Name))
fmt.Println("server:", strings.ToUpper(cnf.Server))
fmt.Println("timeout in minutes:", conf.Timeout / 60)