Despite providing all necessary arguments, the compiler throws an error message about excessive arguments when passing a DataResponse struct as a parameter to JSON().
The provided code excerpt attempts to create an instance of the DataResponse struct and pass it as a parameter to the JSON() function. However, the compiler raises an error indicating too many arguments are given.
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{}
resp := DataResponse(200, user)
JSON(rw, resp)
}
The error occurs due to incorrect syntax for struct initialization. The spaces around the curly braces signify a function call instead of a struct initialization using curly braces. To resolve the issue, change the code as follows:
resp := DataResponse{200, user}
Using curly braces ensures that the code correctly initializes the DataResponse struct with the provided arguments. The compiler will no longer complain about too many arguments.
This modification ensures that the compiler accurately identifies the code as struct initialization and allows the DataResponse struct to be correctly used as a parameter for the JSON() function.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3