Search code examples
gointerface

Golang: A map Interface, how to print key and value


I get a map interface, like :

getUsersAppInfo := usersAppInfo.GetUsersAppInfo(getUserId)

then I print :

fmt.Println(getUsersAppInfo)

get this, like :

[map[appId:2 fcmServerKey:keyTestTest name:com.app]]

Ask : How to just print the value, like

appId value is 2 
name value is com.app 
fcmServerKey:keyTestTest value is keyTestTest 

Solution

  • The OP's comment on the question states that type of getUsersAppInfo is []map[string]interface{}.

    Loop over the slice of maps. For each map, loop over the keys and values and print.

    // loop over elements of slice
    for _, m := range getUsersAppInfo {
    
        // m is a map[string]interface.
        // loop over keys and values in the map.
        for k, v := range m {
            fmt.Println(k, "value is", v)
        }
    }
    

    Run this on the GoLang PlayGround.