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 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Stay tuned working on React Js Cheat Sheets Book

Related Posts