在Go中初始化嵌入式结构体

huangapple go评论76阅读模式
英文:

Initialize embedded struct in Go

问题

我有以下的struct,其中包含一个net/http.Request

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

现在我想在以下函数中初始化匿名内部结构http.Request

func New(origRequest *http.Request, pathParams map[string]string) *MyRequest {
    req := new(MyRequest)
    req.PathParams = pathParams
    return req
}

如何使用参数origRequest初始化内部结构?

英文:

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

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

Now I want to initialize the anonymous inner struct http.Request in the following function:

func New(origRequest *http.Request, pathParams map[string]string) *MyRequest {
    req := new(MyRequest)
    req.PathParams = pathParams
    return req
}

How can I initialize the inner struct with the parameter origRequest?

答案1

得分: 42

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

或者...

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

更多关于嵌入和字段命名的信息,请参阅:http://golang.org/ref/spec#Struct_types。

英文:
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.

答案2

得分: 21

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

它显示,而不是

New(foo, bar)

你可能更喜欢直接使用

&MyRequest{*foo, bar}

英文:

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.

答案3

得分: 7

正如Jeremy在上面展示的那样,匿名字段的“名称”与字段的类型相同。因此,如果x的值是包含匿名int的结构体,那么x.int将引用该字段。

英文:

As Jeremy shows above, the "name" of an anonymous field is the same as the type of the field. So if the value of x were a struct containing an anonymous int, then x.int would refer to that field.

答案4

得分: 1

只为完整起见,我还会添加另一个例子。根据@Victor的评论:

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

func New(origRequest *http.Request, pathParams map[string]string) *MyRequest {
    req := MyRequest{Request: origRequest, PathParams: pathParams}
    return req
}

@Jeffery Martinez的另一个评论进一步澄清了这一点:

"The unqualified type name acts as the field name." So `http.Request` ends up being called just `Request`

以下两个声明是等价的

// Version 1
type MyRequest struct {
    http.Request
    PathParams map[string]string
}

// Version 2
type MyRequest struct {
    Request http.Request
    PathParams map[string]string
}
英文:

Just for completeness sake, I'll add another example as well. Based on the comment from @Victor
> This example shows another way recommended https://play.golang.org/p/Gbn8e6CTVi_c

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

func New(origRequest *http.Request, pathParams map[string]string) *MyRequest {
    req := MyRequest{Request: origRequest, PathParams: pathParams}
    return req
}

Another comment from @Jeffery Martinez helped clarify it further:
> As for how the fields get named: "The unqualified type name acts as the field name." So http.Request ends up being called just Request

i.e: The two following declarations are equivalent

// Version 1
type MyRequest struct {
    http.Request
    PathParams map[string]string
}

// Version 2
type MyRequest struct {
    Request http.Request
    PathParams map[string]string
}

huangapple
  • 本文由 发表于 2012年9月22日 04:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/12537496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定