Reputation: 179
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
Upvotes: 12
Views: 87120
Reputation: 1287
I have found this useful.
https://github.com/yakob-abada/go-api
import "github.com/k0kubun/pp/v3"
m := map[string]string{"foo": "bar", "hello": "world"}
pp.Print(m)
Upvotes: 0
Reputation: 825
I don't recommend to do this in production setup. But when I want to print out a map without too much code on my dev box, i print the JSON serialised version. This will be a crime to do in production.
package main
import (
"encoding/json"
"fmt"
)
func main() {
a := map[string]interface{}{"appId": 2, "fcmServerKey": "keyTestTest", "name": "com.app", "version": []int{1, 2, 3}, "xyz": 3}
bs, _ := json.Marshal(a)
fmt.Println(string(bs))
}
Output:
{"appId":2,"fcmServerKey":"keyTestTest","name":"com.app","version":[1,2,3],"xyz":3}
Upvotes: 17
Reputation:
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.
Upvotes: 23
Reputation: 21902
I'm afraid the only option is to iterate through the map:
getUsersAppInfo := map[string]interface{}{"foo": 3, "bar": "baz"}
for key, value := range getUsersAppInfo {
fmt.Printf("%s value is %v\n", key, value)
}
Upvotes: 4