I want to use the struct DataResponse
as parameter for JSON()
to respond with the user. By initializing an instance of DataResponse
I get the error message, that too many arguments are given, but gave all that are necessary.
type DataResponse struct {
Status int `json:"status"`
Data interface{} `json:"data"`
}
func GetUser(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) {
user := models.User{}
// Fetching user from db
resp := DataResponse(200, user)
JSON(rw, resp) // rw is the ResponseWriter of net/http
}
The following error message is thrown by the compiler:
too many arguments to conversion to DataResponse: DataResponse(200, user)
DataResponse
requires two parameters that are given and Data
is an interface so it should accept models.User
as datatype.
resp := DataResponse(200, user)
The syntax is wrong. Try curly braces for struct initialization:
resp := DataResponse{200, user}
^ ^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With