2016-06-20 7 views
0

私は新しいRequest構造を設計しますが、いくつかのシナリオ(http.Redirect)では、起点を取得する必要がありますhttp.RequestGoで埋め込みフィールドを読み取るには?

私の要求の構造体:

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") 
    } 
+0

'myReq.Request'。 Goのツアーに参加し、Effective Goと言語仕様を読んでください。 – Volker

答えて

3

だがthe language specificationが言うか見てみましょう:

修飾されていないタイプ名は、フィールド名として機能します。

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

関連する問題