我設計了一個新的 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
- 1 回答
- 0 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消