如何访问map [interface {}]界面{}中的键

this is my yaml file:

db:
    # table prefix
    tablePrefix: tbl

    # mysql driver configuration
    mysql:
        host: localhost
        username: root
        password: mysql

    # couchbase driver configuration
    couchbase:
        host: couchbase://localhost

and i use go-yaml library to unmarshall yaml file to variable:

config := make(map[interface{}]interface{})
yaml.Unmarshal(configFile, &config)

config value:

map[mysql:map[host:localhost username:root password:mysql]      couchbase:map[host:couchbase://localhost] tablePrefix:tbl]

how access to db -> mysql -> username value in config without predefined struct type

YAML uses string keys. Have you tried:

config := make(map[string]interface{})

To access nested attributes, use type assertions.

mysql := config["mysql"].(map[string][string])
mysql["host"]

A common pattern is to alias a generic map type.

type M map[string]interface{}

config := make(M)
yaml.Unmarshal(configFile, &config)
mysql := config["mysql"].(M)
host := mysql["host"].(string)

If you don't define the types ahead of time, you need to assert the proper type from each interface{} you encounter:

if db, ok := config["db"].(map[interface{}]interface{}); ok {
    if mysql, ok := db["mysql"].(map[interface{}]interface{}); ok {
        username := mysql["username"].(string)
        // ...
    }
}

https://play.golang.org/p/koSugTzyV-