鉴于这种类型
type Response map[string]interface{}
我创建了一个方法NewResponse,它填充了默认值:
func NewResponse() Response {
resp := Response{"status": 200, "msg": "Added jobs to queue"}
resp_metadata := make(map[string]string)
resp_metadata["base"] = "api/v1"
resp_metadata["self"] = "/crawler/jobs/add"
resp["metadata"] = resp_metadata
return resp
}
我称之为,NewResponse()
但我想Response.New()
代替它,所以方法签名应该像这样
func (Response) New() Response {
但是然后我总是得到错误not enough arguments in call to Response.New
。
那么,如何实现呢?
尽管绝对不是Go语言,但是您可以执行以下操作:
type Response map[string]interface{}
func (r *Response) New() {
*r = make(map[string]interface{})
(*r)["hello"] = "World"
(*r)["high"] = 5
}
func main() {
var r Response
r.New()
for k, v := range r {
fmt.Printf("%s = %v\n", k, v)
}
}
但实际上,没什么错func NewResponse() Response
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句