5

I looking return default value for new struct, without value, this is my current struct :

// Campaign represents a email campaign
type Campaign struct {
    ID             bson.ObjectId   `json:"id" bson:"_id"`
    CampaignName   string          `json:"campaign_name" bson:"campaign_name"`
    FromName       []string        `json:"from_name" bson:"from_name"`
    FromEmail      string          `json:"from_email" bson:"from_email"`
    ReplyEmail     string          `json:"reply_email" bson:"reply_email"`
    Subject        []string        `json:"subject" bson:"subject"`
    BodyText       string          `json:"body_text" bson:"body_text"`
    BodyHTML       string          `json:"body_html" bson:"body_html"`
    SMTPList       bson.ObjectId `json:"smtp_list" bson:"smtp_list"`
    EmailList      bson.ObjectId `json:"email_list" bson:"email_list"`
    DateCreated    time.Time       `json:"date_created" bson:"date_created"`
    DateUpdated    time.Time       `json:"date_updated" bson:"date_updated"`
    DateSendFinish time.Time       `json:"date_send_finish" bson:"date_send_finish"`
    OwnerID        bson.ObjectId   `json:"owner_id" bson:"owner_id"`
    Opens          int             `json:"opens" bson:"opens"`
    Clicks         int             `json:"clicks" bson:"clicks"`
    Status         string          `json:"status" bson:"status"`
}

I want to return Status = "draft" How i can do this ?

Valy
  • 73
  • 1
  • 4

2 Answers2

1

I was able to find another post

One possible idea is to write separate constructor function

// Something is the structure we work with
type Something struct {
     Text string 
     DefaultText string 
} 

// NewSomething create new instance of Something
func NewSomething(text string) Something {
   something := Something{}
   something.Text = text
   something.DefaultText = "default text"
   return something
}

Force a method to get the struct (the constructor way).

A good design is to make your type unexported, but provide an exported constructor function like NewMyType() in which you can properly initialize your struct / type. Also return an interface type and not a concrete type, and the interface should contain everything others want to do with your value. And your concrete type must implement that interface of course.

This can be done by simply make the type itself unexported. You can export the function NewSomething and even the fields Text and DefaultText, but just don't export the struct type something

Another way to customize it for you own module is by using a Config struct to set default values (Option 5 in the link) Not a good way though.

Original Post

Pang
  • 9,564
  • 146
  • 81
  • 122
Ceus
  • 23
  • 2
  • 6
0
// Campaign represents a email campaign
type Campaign struct {
    ID             bson.ObjectId   `json:"id" bson:"_id"`
    CampaignName   string          `json:"campaign_name" bson:"campaign_name"`
    FromName       []string        `json:"from_name" bson:"from_name"`
    FromEmail      string          `json:"from_email" bson:"from_email"`
    ReplyEmail     string          `json:"reply_email" bson:"reply_email"`
    Subject        []string        `json:"subject" bson:"subject"`
    BodyText       string          `json:"body_text" bson:"body_text"`
    BodyHTML       string          `json:"body_html" bson:"body_html"`
    SMTPList       bson.ObjectId `json:"smtp_list" bson:"smtp_list"`
    EmailList      bson.ObjectId `json:"email_list" bson:"email_list"`
    DateCreated    time.Time       `json:"date_created" bson:"date_created"`
    DateUpdated    time.Time       `json:"date_updated" bson:"date_updated"`
    DateSendFinish time.Time       `json:"date_send_finish" bson:"date_send_finish"`
    OwnerID        bson.ObjectId   `json:"owner_id" bson:"owner_id"`
    Opens          int             `json:"opens" bson:"opens"`
    Clicks         int             `json:"clicks" bson:"clicks"`
    Status         string          `json:"status" bson:"status"`
}

Simply You can do like this

campaign.Status = "draft"

or maybe if you want sometype of condition

if true {
campaign.Status = "draft"
} else {
campaign.Status = "sent"
}
Black_Dreams
  • 572
  • 1
  • 5
  • 11