簡體   English   中英

如何在Go中讀取嵌入式字段?

[英]How to read an embedded field in Go?

我設計了一個新的Request結構,但是在某些情況下(http.Redirect),我需要獲取源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")
    }

讓我們看看語言規范怎么說:

非限定類型名稱充當字段名稱。

 // 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 } 

myReq.Request

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM