12

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 
3
  • is []map[string]interface{} Commented May 28, 2020 at 2:48
  • 1
    Fundamental basics of the language are covered in The Tour of Go which you might want to work through once more. Note that in general there are no magic tricks in Go and if you want something to happen you have to write code making this happen. Commented May 28, 2020 at 5:00
  • @卓琮偉 answer to the question you just deleted. See play.golang.org/p/QEsYp1fn0wf. Commented Jul 1, 2020 at 8:05

4 Answers 4

24

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This example will still work, but I thought a slice of maps was more along the lines of getUsersAppInfo := []map[string]interface{}{{"appId": 2, "fcmServerKey": "keyTestTest"}, {"name": "com.app","version": 2, "xyz": 3}}
I thought your example was excellent. It works for more maps than the one map that was listed.
18

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}

3 Comments

why is it a crime in prod? is it slow?
By far the easiest way to print out a map when you're just debugging stuff. Thanks.
@nanakondor because serialising is a costly operation. So if you were to do this, say on a high throughput request (1k> req/s) your instance will be spending quite some time in this operation itself who's sole purpose is for debugging.
5

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)
}

3 Comments

I find a key word "reflect.ValueOf()".
Can I use it? If it can. How to use it?
you can, but you'll end up with the output you don't want. Because that's really what fmt.Println is calling behind the scene for a map. Use the code I gave you instead for the output you want.
0

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)

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.