Url in struct, is this normal?


looking at bellow struct of some librarie. Is this normal, url in struct part? I only saw json or xml, this is new for me. Code should send further then post with json.

type ActivitiesCreateOptions struct {
	Subject      string      `url:"subject,omitempty"`
	Done         uint8       `url:"done,omitempty"`
	Type         string      `url:"type,omitempty"`
	DueDate      string      `url:"due_date,omitempty"`
	DueTime      string      `url:"due_time,omitempty"`
	Duration     string      `url:"duration,omitempty"`
	UserID       uint        `url:"user_id,omitempty"`
	DealID       uint        `url:"user_id,omitempty"`
	PersonID     uint        `url:"person_id,omitempty"`
	Participants interface{} `url:"participants,omitempty"`
	OrgID        uint        `url:"org_id,omitempty"`

Thank you

This is the tags in golang which are discoverable via reflection, like

t := reflect.TypeOf(object)
field := t.Field(0)
//field, _ := t.FieldByName("Age") //alternative

so, this is quite normal, and your library can Marshal/Unmarshal struct using these tags.

1 Like


Thank you for info. Can you please give me exp for to marshal this in json, when you send via http.Newrequest?

Thank you

example at https://play.golang.org/p/70yXwA0R9Ou, i add the json tag to tell json.Marshal how to marshal data. And as you can see, field of string printed is exactly same with definition in tags, and field with tag omitempty will not marshaled if is nil. By the way, the struct have two same tags fields, user_id, corrects in example.

Thank you for this. You have now two tags, one is json and one uri. So in this case β€œuri” tag is just for personal use as mashas/unmarshal can only use json, right?