How to Access Fields Inside map[string]interface 🐹?

March 31, 2022 ¿Ves algún error? Corregir artículo

Starting with the common problem of creating more generic functions, I once dealt with a similar issue: accessing sub-fields within a map[string]interface interface. The solution is much simpler than you think, starting by identifying which field we need within our structure. After that, everything is simpler - you'll need a simple conversion of the field that has a sub-field to a map[string]interface interface and then access the field you need. Finally, you need to convert the value to your desired type. For this example, I don't use validation for field existence, but it's highly recommended.

main.go
package main import "fmt" func main() { subMap := map[string]interface{}{ "sqlid": 1, } Map := map[string]interface{}{ "metadata": subMap, } sqlid := Map["metadata"].(map[string]interface{})["sqlid"].(int) fmt.Println(sqlid) }

I hope this brief article helps you easily solve this problem in the future. Happy coding and save this article so you never lose the solution.