json unmarshalling

Fix the issue below to assure result.status is printed as 200

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Result struct {
  7. status int
  8. }
  9. func main() {
  10. var data = []byte(`{"status": 200}`)
  11. result := &Result{}
  12. if err := json.Unmarshal(data, result); err != nil {
  13. fmt.Println("error:", err)
  14. return
  15. }
  16. fmt.Printf("result=%+v", result)
  17. }

Answer

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Result struct {
  7. Status int `json:"status"`
  8. }
  9. func main() {
  10. var data = []byte(`{"status": 200}`)
  11. result := &Result{}
  12. if err := json.Unmarshal(data, result); err != nil {
  13. fmt.Println("error:", err)
  14. return
  15. }
  16. fmt.Printf("result=%+v", result)
  17. }