如何访问接口的属性 [英] How to access attribute of interface

查看:176
本文介绍了如何访问接口的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在两个响应结构的标题和正文中使用HTTP状态码。如果没有设置状态码两次作为函数参数,并且再次为结构设置以避免冗余。 $ b

JSON()的参数响应是一个接口允许两个结构被接受。编译器会抛出以下异常:

$ p $ response.Status undefined(type interface {}没有字段或方法状态)

,因为响应字段不能有状态属性。有没有其他方法来避免设置状态码两次?

 类型响应struct {
状态int`json:status`
数据接口{}` json:data`
}

type ErrorResponse struct {
Status int`json:status`
Errors [] string`json:errors `
}

func JSON(rw http.ResponseWriter,response interface {}){
payload,_:= json.MarshalIndent(response,,)
rw.WriteHeader(response.Status)
...
}


rw.WriteHeader(response.Status)中的响应类型是界面{} 。在Go中,你需要显式声明底层结构的类型,然后访问该字段:

  func JSON(rw http。 ResponseWriter,响应接口{}){
有效载荷,_:= json.MarshalIndent(响应,,)
开关r:=响应(类型){$ b $ case ErrorResponse:
rw.WriteHeader(r.Status)
case响应:
rw.WriteHeader(r.Status)
}
...
}

然而,更好的方法是为您的响应定义一个通用界面,获取响应状态的方法:

pre $ type Statuser接口{
Status()int
}

//您需要重新命名字段以避免名称冲突。
func(r响应)状态()int {返回r.ResStatus}
func(r ErrorResponse)状态()int {返回r.ResStatus}

func JSON(rw http.ResponseWriter,响应Statuser){
有效载荷,_:= json.MarshalIndent(response,,)
rw.WriteHeader(response.Status())
...
}

最好重命名 Response DataResponse ResponseInterface 响应,IMO 。


It was my intention to use the HTTP status codes both in the header and the body of the two response structs. Bu that without setting the status code twice as function parameter and again for the struct to avoid redundancy.

The parameter response of JSON() is an interface to allow both structs to be accepted. The compiler throws the following exception:

response.Status undefined (type interface {} has no field or method Status)

because the response field must not have a status attribute. Is there an alternative way to avoid setting the status code twice?

type Response struct {
    Status int         `json:"status"`
    Data   interface{} `json:"data"`
}

type ErrorResponse struct {
    Status int      `json:"status"`
    Errors []string `json:"errors"`
}

func JSON(rw http.ResponseWriter, response interface{}) {
    payload, _ := json.MarshalIndent(response, "", "    ")
    rw.WriteHeader(response.Status)
    ...
}

解决方案

The type response in rw.WriteHeader(response.Status) is interface{}. In Go, you need to explicitly assert the type of the underlying struct and then access the field:

func JSON(rw http.ResponseWriter, response interface{}) {
    payload, _ := json.MarshalIndent(response, "", "    ")
    switch r := response.(type) {
    case ErrorResponse:
        rw.WriteHeader(r.Status)
    case Response:
        rw.WriteHeader(r.Status) 
    }
    ...
}

A better and the preferred way to do this however is to define a common interface for your responses, that has a method for getting the status of the response:

type Statuser interface {
    Status() int
}

// You need to rename the fields to avoid name collision.
func (r Response) Status() int { return r.ResStatus }
func (r ErrorResponse) Status() int { return r.ResStatus }

func JSON(rw http.ResponseWriter, response Statuser) {
    payload, _ := json.MarshalIndent(response, "", "    ")
    rw.WriteHeader(response.Status())
    ...
}

And it's better to rename Response to DataResponse and ResponseInterface to Response, IMO.

这篇关于如何访问接口的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆