Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler: too many arguments given despite that all are given

Tags:

go

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.

like image 980
user3147268 Avatar asked May 20 '15 19:05

user3147268


1 Answers

resp := DataResponse(200, user)

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

resp := DataResponse{200, user}
                    ^         ^
like image 82
cnicutar Avatar answered Nov 12 '22 16:11

cnicutar