user3147268
user3147268

Reputation: 1884

Compiler: too many arguments given despite that all are given

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.

Upvotes: 11

Views: 11924

Answers (1)

cnicutar
cnicutar

Reputation: 182639

resp := DataResponse(200, user)

The syntax is wrong. Try curly braces for struct initialization:

resp := DataResponse{200, user}
                    ^         ^

Upvotes: 38

Related Questions