Use the encoding/json package's json.Unmarshal function to convert JSON data into Go variables. Pass a byte slice of the JSON and a pointer to the target variable to populate it directly.
package main
import (
"encoding/json"
"fmt"
)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
jsonData := []byte(`{"name":"Alice","age":30}`)
var u User
err := json.Unmarshal(jsonData, &u)
if err != nil {
panic(err)
}
fmt.Println(u.Name, u.Age)
}