我已经创建了一个具有相同形状的结构,除了该结构的名称之外:
type Response struct {
code int
body string
}
type Request struct {
code int
body string
}
问题是,是否存在一种抽象结构体的方法?
例如:
type Response struct {
Payload
}
type Request struct {
Payload
}
type Payload struct {
code int
body string
}
例如,当我在此处创建新结构时
a := Response{ Payload { code:200, body: "Hello world" } }
但是我想省略每次写Payload
为:
a := Response{ code:200, body: "Hello world" }
是否可以将一个结构嵌入另一个结构并忽略该结构的名称?
我在操场上尝试了以下代码,并且可以正常工作,也许这正是您想要的:https://play.golang.org/p/3c8lsNyV9_1