package main
import (
"encoding/json"
"fmt"
)
type Product struct {
Id int
Name string
Price float32
Status bool
}
func StructToMap(obj interface{}) (mymap map[string]interface{}, err error) {
data, err := json.Marshal(obj)
if err != nil {
return
}
err = json.Unmarshal(data, &mymap)
return
}
func main() {
product := Product{
Id: 1,
Name: "Name 1",
Price: 5.6,
Status: true,
}
mymap, err := StructToMap(product)
if err != nil {
fmt.Printf(err.Error())
} else {
fmt.Println(mymap)
}
}
map[Id:1 Name:Name 1 Price:5.6 Status:true]