Initialize embedded struct in Go

后端 未结 3 911
情话喂你
情话喂你 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:52

    req := new(MyRequest)
    req.PathParams = pathParams
    req.Request = origRequest
    

    or...

    req := &MyRequest{
      PathParams: pathParams
      Request: origRequest
    }
    

    See: http://golang.org/ref/spec#Struct_types for more about embedding and how the fields get named.

提交回复
热议问题