为了账号安全,请及时绑定邮箱和手机立即绑定

如何读取 Go 中的嵌入字段?

如何读取 Go 中的嵌入字段?

Go
牛魔王的故事 2022-01-17 19:53:51
我设计了一个新的 Request 结构,但在某些情况下(http.Redirect),我需要获取 origin http.Request。我的请求结构:type Request struct {    *http.Request}func (r *Request) IsGet() bool {    return strings.EqualFold("GET", r.Method)}主功能:    req := http.Request{        Method:"POST",    }    myReq := &Request{&req}    // How to get original request.    originalReq, ok := (interface{}(*myReq)).(http.Request);    if ok {        fmt.Printf("Method: %s\n", originalReq.Method)    } else {        fmt.Println("Failure")    }
查看完整描述

1 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

让我们看看语言规范是怎么说的:


非限定类型名称充当字段名称。


// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4

struct {

  T1        // field name is T1

  *T2       // field name is T2

  P.T3      // field name is T3

  *P.T4     // field name is T4

  x, y int  // field names are x and y

}

IE myReq.Request


查看完整回答
反对 回复 2022-01-17
  • 1 回答
  • 0 关注
  • 222 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号