Hello,
Once I GET the info from the next operate, I can get it printed out utilizing fmt.Println nevertheless it within the browser and within the postman I get following.
[
{},
{},
{}
]
The operate is the next.
func sendResponse(w http.ResponseWriter, statusCode int, payload interface{}){
fmt.Println(“Payload Unmarshal “,payload)
// prints this >> Payload Unmarshal [{1 Java} {2 python} {3 golang}]response, _ := json.Marshal(payload)
w.Header().Set(“Content material-Sort”,“utility/json; charset=utf-8”)
w.WriteHeader(statusCode)
fmt.Println(“From sendResponse app “,response)
// Prints this >> From sendResponse app [91 123 125 44 123 125 44 123 125 93]w.Write(response)
}
Can anybody assist me on this regard, what incorrect is finished within the code ?
Very first thing I see: you possibly can be swallowing an error. Change this:
// From this...
response, _ := json.Marshal(payload)
// ... to this:
response, err := json.Marshal(payload)
if err != nil {
fmt.Println("error:", err)
}
If you wish to see what the json.Marshal’d string truly appears like you possibly can additionally change this line:
// From this...
fmt.Println("From sendResponse app ",response)
// ... to this:
fmt.Println("From sendResponse app ", string(response))
Strive that and tell us what you discover.
Thanks for the reply, however It didn’t assist ![]()
fmt.Println("From sendResponse app ",response)
// Payload Unmarshal [{1 Java} {2 python} {3 golang}]
fmt.Println("From sendResponse app ", string(response))
// From sendResponse app [{},{},{}]
Invalid JSON!??
Error: Parse error on line 1: [{1 Java} {2 python} { –^ Expecting ‘STRING’, ‘}’, got ‘NUMBER’
Nopes, this (Invalid JSON) is not the case !
What’s the type on payload? I suspect your problem is coming from code that isn’t visible here. Consider the following:
package main
import (
"encoding/json"
"fmt"
)
type Payload struct {
ID int
Value string
}
func main() {
payload := []Payload{{1, "Java"}, {2, "python"}, {3, "golang"}}
fmt.Println(payload)
response, err := json.Marshal(payload)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(string(response))
}
… which produces:
[{1 Java} {2 python} {3 golang}]
[{"ID":1,"Value":"Java"},{"ID":2,"Value":"python"},{"ID":3,"Value":"golang"}]
You’ll be able to run it your self within the playground. In the event you can tweak this to offer a reproducible instance someone right here may have the opportunity that will help you.
Thanks for replying and on your consideration. The code I’m engaged on is right here.
All what it’s best to do with the intention to run the applying is to run the docker-compose up and entry the localhost:8080/languages to get all of the languages.
Didn’t must run it. Already discovered the issue:
sort language struct {
id int `json:"id"`
title string `json:"title"`
}
These fields aren’t exported. Change this to:
sort language struct {
ID int `json:"id"`
Identify string `json:"title"`
}
2 Likes
Bundle of thanks, it really works now !
I didn’t take note of that ![]()
Thanks as soon as once more ![]()

