编译器:尽管给出了所有参数,但给出了太多参数 [英] Compiler: too many arguments given despite that all are given
本文介绍了编译器:尽管给出了所有参数,但给出了太多参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 DataResponse
结构作为 JSON()
的参数来与用户响应.通过初始化 DataResponse
的实例,我得到了错误消息,给出了太多的参数,但给出了所有必需的参数.
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
需要给定两个参数,并且 Data
是一个接口,因此它应该接受 models.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}
^ ^
这篇关于编译器:尽管给出了所有参数,但给出了太多参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文