我设计了一个新的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")
}
最佳答案
让我们看看是什么the language specification说:
The unqualified type name acts as the field name.
// 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
关于go - 如何读取 Go 中的嵌入式字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37917109/