Initialize embedded struct in Go

后端 未结 3 918
情话喂你
情话喂你 2021-02-03 20:16

I have the following struct which contains a net/http.Request:

type MyRequest struct {
    http.Request
    PathParams map[string]strin         


        
3条回答
  •  情深已故
    2021-02-03 20:55

    What about:

    func New(origRequest *http.Request, pathParams map[string]string) *MyRequest {
            return &MyRequest{*origRequest, pathParams}
    }
    

    It shows that instead of

    New(foo, bar)
    

    you might prefer just

    &MyRequest{*foo, bar}
    

    directly.

提交回复
热议问题