-4

i have a error to print the data of json file with structs, when i print the result in empty

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
)

type Users struct {
    Users []User `json:"users"`
}

type User struct {
    name     string `json:"name"`
    lastname string `json:"lastname"`
    phone    string `json:"phone"`
}

func main() {
    file, _ := ioutil.ReadFile("./test.json")

    data := Users{}

    json.Unmarshal([]byte(file), &data)

    for i := 0; i < len(data.Users); i++ {
        fmt.Println(data.Users[i].name)
        fmt.Println(data.Users[i].lastname)
        fmt.Println(data.Users[i].phone)
    }
}

this is the result when i run me code img

  • 1
    This is an English language site. Please ask your question in that language, or ask at [es.so] instead. – Ken White May 30 '22 at 23:22
  • 2
    `User` struct fields should be exported (capital letter first): `Name`, `Lastname`, etc – zerkms May 30 '22 at 23:46
  • 1
    The 1st thing you need to do is check the error return value from `ReadFile` and `Unmarshal`. More than likely the file does not exist (in the directory you are running in), is empty, or is not valid JSON. Plus as @zerkms says unexported fields are not unmarshaled. This is basic stuff. – AJR May 30 '22 at 23:51
  • Duplicate of: [1](https://stackoverflow.com/questions/26327391), [2](https://stackoverflow.com/questions/11126793), [3](https://stackoverflow.com/questions/25595096), [4](https://stackoverflow.com/questions/28467302), [5](https://stackoverflow.com/questions/38093012) – blackgreen May 31 '22 at 06:47

0 Answers0