Compiler: too many arguments given despite that all are given

空扰寡人 提交于 2021-02-04 17:39:08

问题


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.


回答1:


resp := DataResponse(200, user)

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

resp := DataResponse{200, user}
                    ^         ^


来源:https://stackoverflow.com/questions/30358313/compiler-too-many-arguments-given-despite-that-all-are-given

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!