converter mapa para struct Golang
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Address struct {
Street string `json:"street"`
Suite string `json:"suite"`
Zipcode string `json:"zipcode"`
}
func ConvertMapToStructArray() {
url := "https://jsonplaceholder.typicode.com/users"
storeData := make([]map[string]interface{}, 1)
var newStoreData []struct {
ID uint `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
Website string `json:"website"`
Address Address `json:"address"`
}
req, _ := http.Get(url)
json.NewDecoder(req.Body).Decode(&storeData)
defer req.Body.Close()
stringify, _ := json.Marshal(storeData)
json.Unmarshal(stringify, &newStoreData)
fmt.Printf("ConvertMapToStructArray %#v \n", newStoreData)
}
func main() {
ConvertMapToStructArray()
}
Restu Wahyu Saputra