sách gpt4 ăn đã đi

inheritance - 转到 : How to reference a field in an inherited struct

In lại 作者:IT王子 更新时间:2023-10-29 02:29:32 27 4
mua khóa gpt4 giày nike

我有 2 个结构,其中一个继承了由 type Common struct {...} 表示的所有结构中共有的值

type Common struct{
Bàn tay của bạn
Được tạo ra vào thời điểm.Thời gian
UpdatedAt time.Time
CreatorId int
}

type Post struct{
type Post struct{
Chung
Trạng thái
Title string
ShortDescription string
Content string
CategoryIds []int
TagIds []int
Url string
MainImageId int
Keywords []string
}

但是,当我尝试创建 Post 结构的新实例时,如下所示。

post1 := &Post{
CreatorId: 1,
Status: 1,
Title: "this is the title of the first post",
ShortDescription: "this is the short description of this post",
Content: "",
Url: "first-post",
MainImageId: 1,
}

它不识别 CreatorId 字段。我如何在新实例中引用此字段或修改结构,以便将 CreatorID 注册为 Bưu kiện 结构的一部分?谢谢

câu trả lời hay nhất

CreatorId(顺便说一句,应该称为 CreatorID)是 Сommon 的一部分,因此唯一的初始化方法是结构文字是:

post1 := &Post{
Common: Common{CreatorID: 1},
// ...
}

或者,

post1 := &Post{
// ...
}
post1.CreatorID = 1

关于inheritance - 转到 : How to reference a field in an inherited struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39618959/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress