Is there a spread operator for golang structs

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

Is there a spread operator for golang structs

问题

以下是翻译好的内容:

有以下的结构体,其中PostInputcreatePost函数的参数。

  1. type PostInput struct {
  2. Title string
  3. Content string
  4. }
  5. type PostInputWithTime struct {
  6. Title string
  7. Content string
  8. CreatedAt time.Time
  9. UpdatedAt time.Time
  10. }

但是不希望将CreatedAtUpdatedAt暴露给用户,所以我将它们添加到函数内部,如下所示。

  1. func createPost(input PostInput) {
  2. updatedInput := PostInputWithTime{
  3. Title: input.Title,
  4. Content: input.Content,
  5. CreatedAt: time.Now(),
  6. UpdatedAt: time.Now(),
  7. }
  8. db.InsertOne(updatedInput)
  9. }

这样做是可以的,但我想知道是否有更优雅的方法?我知道可以在一个结构体中嵌入另一个结构体,但不能在根层级上嵌入(类似于JavaScript的扩展运算符)。

  1. // 类似于这样
  2. type PostInputWithTime struct {
  3. ...PostInput
  4. CreatedAt
  5. UpdatedAt
  6. }
英文:

Have the following structs where PostInput is a param for a createPost function.

  1. type PostInput struct {
  2. Title String
  3. Content String
  4. }
  5. type PostInputWithTime struct {
  6. Title String
  7. Content String
  8. CreatedAt Time
  9. UpdatedAt Time
  10. }

But do not want CreatedAt and UpdatedAt to be exposed to users so i add it inside the function like so.

  1. func createPost(input PostInput) {
  2. updatedInput = PostInputWithTime{
  3. Title: input.Title
  4. Content: input.Content
  5. CreatedAt: time.Now()
  6. UpdatedAt: time.Now()
  7. }
  8. db.InsertOne(updatedInput)
  9. }

It is working fine but curious if there is a more elegant way to do it? I know it's possible to embed struct on another struct but not on the root layer (like javascript spread operator).

  1. // something like this
  2. type PostInputWithTime struct {
  3. ...PostInput
  4. CreatedAt
  5. UpdatedAt
  6. }

答案1

得分: 1

在Go语言中,没有像JavaScript中的spread操作符一样的用于结构体的spread操作符。你可以使用嵌入、复制数值或者实现一些基于反射的魔法来达到类似的效果,但是没有直接的spread操作符可用。

英文:

> Is there a spread operator for go[...] structs [...] like javascript spread operator [?]

No.

(You either have to use embedding, copy the values or implement some reflect-based magic, but no, no spread.)

答案2

得分: 0

  1. type PostInput struct {
  2. Title string
  3. Content string
  4. }
  5. type PostInputWithTime struct {
  6. PostInput //结构体嵌入
  7. CreatedAt time.Time
  8. UpdatedAt time.Time
  9. }
  1. type PostInput struct {
  2. Title string
  3. Content string
  4. }
  5. type PostInputWithTime struct {
  6. PostInput //Struct Embedding
  7. CreatedAt time.Time
  8. UpdatedAt time.Time
  9. }
英文:
  1. type PostInput struct {
  2. Title string
  3. Content string
  4. }
  5. type PostInputWithTime struct {
  6. PostInput //Struct Embedding
  7. CreatedAt time.Time
  8. UpdatedAt time.Time
  9. }

huangapple
  • 本文由 发表于 2023年3月15日 22:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75746090.html
匿名

发表评论

匿名网友

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

确定