How to initialize the field of struct refrencing field of other struct in Go

Viewed 34

I have two structs

type Service struct {
    

    Label string `json:"label" gorm:"type:varchar(50)"`
    Code  string `json:"code" gorm:"type:varchar(50)"`

    my.Active1
    
}

And in other package

package my
type Active1 struct {
    IsActive uint8 `json:"is_active" gorm:"type:tinyint(1) unsigned;not null;default:1;index"`
}

While instantiating struct ,I can easily do something like

s := entity.Service{
        Label:      payload.Label,
        Code:       payload.Code,
        my.Active1:  // how to initialize this field ??
    }

I am not getting any way to initialize the referenced field ,how to do so any idea ?

1 Answers

Initializing a struct with another nested struct can be done using a struct literal like so:

s := entity.Service{
        Label:      payload.Label,
        Code:       payload.Code,
        my.Active1:  my.Active1{ 
            IsActive: uint8(1),
        },
    }

Here's an example in Go Playground.

Related