0

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.
This is the code:

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"
}


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

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


How do I achieve this same code from Laravel to Golang?
class Dummy extends Model
{
    protected $appends = ['image_url'];

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

Please pardon me I am still learning Golang, thank you
radren
  • 154
  • 2
  • 14

1 Answers1

1

You are not far off..

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

    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"`
    }

Then define a method with a receiver of type Dummy pointer

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

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

sbrichards
  • 2,169
  • 2
  • 19
  • 32
  • Thanks this is almost what I need, but how to automatically add the ImageUrl attribute whenever I call the Dummy struct? this is the example https://play.golang.com/p/RrjQ4Z5-UrR , I expect it to at least have "test" returned from the empty initialized struct – radren Feb 23 '22 at 05:36
  • 1
    https://play.golang.com/p/zHwWdS3dFiI Since you have JSON tags, using json.Unmarshal would also set the values in the struct for you instead of having to instantiate them yourselves. You cannot 'call' a struct, a struct is a type, and methods can be defined on the struct. In your usage, you'd likely unmarshal some JSON into that struct, then have methods to manipulate fields in the struct, getters/setters, etc. You dont have to use getter/setter methods though, depending on the scope. – sbrichards Feb 23 '22 at 05:42
  • 1
    @radren are you trying to set a default value for the ImageUrl, so that every instance has the default unless it is overriden with values from the DB? Go doesn't do default values the same way you might get in PHP. I think the constructor, NewDummy() is a good way to go. You can check if empty and set the default value, before returning the new struct. Other options: https://stackoverflow.com/questions/37135193/how-to-set-default-values-in-go-structs – Brian Wagner Feb 24 '22 at 16:35
  • @BrianWagner Yep, I am trying to do so, but I don't think I can so I decided to use the setter and getter instead, also thanks for the link, I'll look it up – radren Feb 27 '22 at 01:10