Notes
  • Overview
  • Go
    • Basics
    • How To?
    • Tools & Libraries
    • Tutorials
    • Go Modules
    • Design Patterns
    • Guidelines
      • Code Review and Comments
    • Conversion
      • map[string]interface{} to JSON
      • map[string]interface{} to struct
    • Tips & Tricks
  • Docker
    • Blog Posts
    • Commands
  • Kubernetes
    • Courses
    • Concepts
    • Networking
      • Ingress Controller
    • KIND
    • Custom Resources
    • Tips & Tricks
      • Commands
        • Log
        • List
      • Container
        • Inject Executable Script
      • RBAC
        • Cross namespaced access
      • Patching Object
        • Restrict Key
      • Copy Object
  • Git
    • Rebase
  • System Design
    • Pub-Sub
      • Ovserver vs PubSub
  • Distributed System
    • Untitled
  • Linux
    • Bash
      • Basics
      • How To?
      • Pipe
      • Tips & Tricks
  • Security
    • Untitled
  • Database
    • PostgreSQL
      • Tricks & Tips
    • MySQL
      • Tips & Tricks
  • MISC
    • Development Patterns
    • Useful Github Apps/Actions
    • Parameters vs Arguments
    • Open API
  • Terminal
  • VS Code
Powered by GitBook
On this page
  • Conversion
  • map[string]interface{} to struct
  • struct to map[string]interface{}

Was this helpful?

  1. Go
  2. Conversion

map[string]interface{} to struct

Conversion

map[string]interface{} to struct

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    // map data
    mapData := map[string]interface{}{
        "Name": "noknow",
        "Age": 2,
        "Admin": true,
        "Hobbies": []string{"IT","Travel"},
        "Address": map[string]interface{}{
            "PostalCode": 1111,
            "Country": "Japan",
        },
        "Null": nil,
    }

    // struct - Need to be defined according to the above map data.
    type Addr struct {
        PostalCode int
        Country string
    }
    type Me struct {
        Name string
        Age int
        Admin bool
        Hobbies []string
        Address Addr
        Null interface{}
    }

    // Convert map to json string
    jsonStr, err := json.Marshal(mapData)
    if err != nil {
        fmt.Println(err)
    }

    // Convert json string to struct
    var me Me
    if err := json.Unmarshal(jsonStr, &me); err != nil {
        fmt.Println(err)
    }

    // Output
    fmt.Printf("Name: %s
Age: %d
Admin: %t
Hobbies: %v
Address: %v
Null: %v
", me.Name, me.Age, me.Admin, me.Hobbies, me.Address, me.Null)
}

struct to map[string]interface{}

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    // struct data
    type Addr struct {
        PostalCode int
        Country string
    }
    type Me struct {
        Name string
        Age int
        Admin bool
        Hobbies []string
        Address Addr
        Null interface{}
    }
    addr := Addr{
        PostalCode: 1111,
        Country: "Japan",
    }
    me := Me{
        Name: "noknow",
        Age: 2,
        Admin: true,
        Hobbies: []string{"IT","Travel"},
        Address: addr,
        Null: nil,
    }

    // Convert map to json string
    jsonStr, err := json.Marshal(me)
    if err != nil {
        fmt.Println(err)
    }

    // Convert struct
    var mapData map[string]interface{}
    if err := json.Unmarshal(jsonStr, &mapData); err != nil {
        fmt.Println(err)
    }

    // Output
    fmt.Printf("Name: %v (%T)
Age: %v (%T)
Admin: %v (%T)
Hobbies: %v (%T)
Address: %v (%T)
Null: %v (%T)
", mapData["Name"], mapData["Name"], mapData["Age"], mapData["Age"], mapData["Admin"], mapData["Admin"], mapData["Hobbies"], mapData["Hobbies"], mapData["Address"], mapData["Address"], mapData["Null"], mapData["Null"])
}
Previousmap[string]interface{} to JSONNextTips & Tricks

Last updated 4 years ago

Was this helpful?

Ref:

Ref:

https://noknow.info/it/go/how_to_conveert_between_map_string_interface_and_struct#sec1
https://noknow.info/it/go/how_to_conveert_between_map_string_interface_and_struct#sec2