Gaurav Garg
Gaurav Garg

Reputation: 137

How can i print Unmarshalled data in golang?

I am reading json from Raabbitmq in golang and mapping the json in an interface

My struct looks like this, and

type Documents struct {
    user_id    string
    partner_id []string
    last_login int
}

and I am mapping the incoming json in the above struct, but for debugginf purpose, i want to see the interface array , how can i print the mapped data array (body in my case )

        var body []Documents
        json.Unmarshal(d.Body, &body)

        log.Printf("Received a message: %s", body)

Do i need to put other identifier instead of %s ?

Upvotes: 2

Views: 2855

Answers (1)

jeevatkm
jeevatkm

Reputation: 4781

You have problem with your struct definition. You need use Exported identifier, like-

type Documents struct {
    UserID    string    `json:"user_id"`
    PartnerID []string  `json:"partner_id"`
    LastLogin int       `json:"last_login"`
}

For your question, refer to format printing verbs.

To print values of body-

log.Printf("Received a message: %v", body)

To print values along with variable name -

log.Printf("Received a message: %#v", body)

Upvotes: 5

Related Questions