-1

I doing a simple rest service with gorilla/mux and mysql database

type Carro struct{
  Ano int `json:"ano" `
  Cor string `json:"cor" `
  Nome string`json:"nome" "`
}

type Revenda struct{
  carro []Carro
  roda string
}

func test(w http.ResponseWriter, r *http.Request) {
 var listas []Carro
 carA := Carro{1975,"Amarelo","Fusca"}
 listas =append(listas,carA)
 carB := Carro{1972,"Azul","Fusca"}
 listas =append(listas,carB)
 revenda := Revenda{carro:listas,roda:"branca"}
 json.NewEncoder(w).Encode(revenda)
}

and the return is only

{}

What am I doing wrong?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

1

You are doing wrong inside to create struct Revenda, where you declare fields of this struct with small letter but J Son package wants fields should be exported(variable first letter should be start with capital letter) that's why its not create J Son for this, So try below code I've modified using your previous code.

 type Carro struct {
        Ano  int    `json:"ano"`
        Cor  string `json:"cor"`
        Nome string `json:"nome"`
    }

    type Revenda struct {
        Carro []Carro `json:"carro"`
        Roda  string  `json:"roda"`
    }

var listas []Carro
    carA := Carro{1975, "Amarelo", "Fusca"}
    listas = append(listas, carA)
    carB := Carro{1972, "Azul", "Fusca"}
    listas = append(listas, carB)
    revenda := Revenda{Carro: listas, Roda: "branca"}
    fmt.Println(revenda)
    json.NewEncoder(w).Encode(revenda)
saddam
  • 809
  • 8
  • 23