Trying to get values from map

Hello,

I lost one day trying to parse a map to get values but I did get a solution to do it.

Here is my map

map[
@odata.context:https://google.fr)
value:[
map[
@odata.etag: “3766423”
candidat1:
candidat2:aa9dfcea-e4ae-4087-95da-5352f8b7efaa
entreprise1:35252bb8-5434-ef11-8409-7c1e5220b4fd
]
]
]

What i am trying to do is get candidat1, candidat2 and entreprise1 but i am facing a problem where the second map is in fact an array of map in value field.

How could I get thoses values plz ?

Which is your map definition ?

hello,

I do not have a struct to define this map. this structure will not change and wanted to get values without using a struct

from your code snippet, sounds your map like this
map[string]map[id]map[string]string
for example :grinning:
map[“google.fr”]map[3766423]map[“canditat1”]=“”
map[“google.fr”]map[3766423]map[“canditat2”]=“aa9dfcea-e4ae-4087-95da-5352f8b7efaa”
map[“google.fr”]map[3766423]map[“entreprise1”]=“35252bb8-5434-ef11-8409-7c1e5220b4fd”

so if you want fo search for canditat2
you should
var myMap map[string]map[id]map[string]string
try this code

package main

import (
    "fmt"
)

func main() {
    // Define the nested map
    myMap := map[string]map[int]map[string]string{
        "google.fr": {
            3766423: {
                "canditat1":   "",
                "canditat2":   "aa9dfcea-e4ae-4087-95da-5352f8b7efaa",
                "entreprise1": "35252bb8-5434-ef11-8409-7c1e5220b4fd",
            },
        },
    }

    // Search for "entreprise1"
    searchKey := "entreprise1"
    found := false
    for domain, idMap := range myMap {
        for id, innerMap := range idMap {
            if value, exists := innerMap[searchKey]; exists {
                fmt.Printf("Found %s: %s in domain: %s with ID: %d\n", searchKey, value, domain, id)
                found = true
            }
        }
    }

    if !found {
        fmt.Printf("%s not found in the map\n", searchKey)
    }
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.