How to print map string interface in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

Today, we will learn to create and print map string interface in golang, here we will use map to create, json to marshal, and fmt to print packages.

So, first, we will create the m variable and assign map[string]interface{} data to it.

Second, we will marshal it to convert into a byte array, and in the last step, we will print it using the fmt.Println() method.

Let’s dive into code.

package main

import (
	"fmt"
	"log"
	"encoding/json"
)

func main() {
	m := map[string]string{
   		"USER_ID":"JD", 
		"USER_NAME":"John Doe",
	}
	b, err := json.Marshal(m)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b))
}

When you run it you will get JSON string something like check in output console in following image.

print, map string interface, golang

Try it yourself

All the best 👍

Follow me on Twitter