如何在Golang结构体中添加自定义属性,类似于Laravel模型上的$append?

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

How to append custom attribute to Golang struct like $append on a Laravel model?

问题

我正在尝试在我的Golang结构体中添加自定义属性,就像我通常在Laravel模型上使用$appends变量添加自定义属性一样。

这是代码:

package models

import (
	"time"
)

type Dummy struct {
	ID          string    `json:"id" gorm:"primary_key"`
	Description string    `json:"description"`
	Image       string    `json:"image"`
	ImageUrl    ImageUrlDummy
	Number      int       `json:"number"`
	UserId      string    `json:"user_id"`
	User        User      `json:"user"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

func ImageUrlDummy() string {
	return "test"
}

然而,结构体中的ImageUrlDummy不起作用,它返回错误,例如:

ImageUrlDummy (value of type func() string) is not a type

我该如何在Golang中实现与Laravel相同的代码?

type Dummy struct {
	ID          string    `json:"id" gorm:"primary_key"`
	Description string    `json:"description"`
	Image       string    `json:"image"`
	Number      int       `json:"number"`
	UserId      string    `json:"user_id"`
	User        User      `json:"user"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

func (d *Dummy) ImageUrl() string {
	return "this is the image url"
}

请原谅我,我还在学习Golang,谢谢。

英文:

I am trying to add a custom attribute to my Golang struct just like how I usually add custom attribute on a Laravel model using the $appends variable.
<br>
This is the code:

package models

import (
	&quot;time&quot;
)

type Dummy struct {
	ID          string `json:&quot;id&quot; gorm:&quot;primary_key&quot;`
	Description string `json:&quot;description&quot;`
	Image       string `json:&quot;image&quot;`
	ImageUrl    ImageUrlDummy
	Number      int       `json:&quot;number&quot;`
	UserId      string    `json:&quot;user_id&quot;`
	User        User      `json:&quot;user&quot;`
	CreatedAt   time.Time `json:&quot;created_at&quot;`
	UpdatedAt   time.Time `json:&quot;updated_at&quot;`
}

func ImageUrlDummy() string {
	return &quot;test&quot;
}

<br>
However, the ImageUrlDummy inside the struct does not work, it return error such as:


ImageUrlDummy (value of type func() string) is not a type

<br>
How do I achieve this same code from Laravel to Golang?

class Dummy extends Model
{
    protected $appends = [&#39;image_url&#39;];

    public function getImageUrlAttribute(){
        return &quot;this is the image url&quot;;
    }
}

<br>
Please pardon me I am still learning Golang, thank you

答案1

得分: 1

你离正确的不远了。

将你的结构体改为以下形式(删除ImageUrlDummy,修复Image字段的json标签):

    type Dummy struct {
        ID          string    `json:"id" gorm:"primary_key"`
        Description string    `json:"description"`
        Image       string    `json:"image"`
        Number      int       `json:"number"`
        UserId      string    `json:"user_id"`
        User        User      `json:"user"`
        CreatedAt   time.Time `json:"created_at"`
        UpdatedAt   time.Time `json:"updated_at"`
    }

然后定义一个带有Dummy指针类型接收器的方法:

    func (d *Dummy) ImageUrl() string {
        return "test"
    }

这里有一个更完整的示例:https://play.golang.com/p/olGSFhBgqkG

英文:

You are not far off..

Change your struct to (remove ImageUrlDummy, fix json tag for Image):

    type Dummy struct {
        ID          string `json:&quot;id&quot; gorm:&quot;primary_key&quot;`
        Description string `json:&quot;description&quot;`
        Image       string `json:&quot;image&quot;`
        Number      int       `json:&quot;number&quot;`
        UserId      string    `json:&quot;user_id&quot;`
        User        User      `json:&quot;user&quot;`
        CreatedAt   time.Time `json:&quot;created_at&quot;`
        UpdatedAt   time.Time `json:&quot;updated_at&quot;`
    }

Then define a method with a receiver of type Dummy pointer

    func (d *Dummy) ImageUrl() string {
        return &quot;test&quot;
    }

Example with a few more things: https://play.golang.com/p/olGSFhBgqkG

huangapple
  • 本文由 发表于 2022年2月23日 12:34:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/71231421.html
匿名

发表评论

匿名网友

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

确定