A
A
Adel1ne2017-06-20 12:20:20
go
Adel1ne, 2017-06-20 12:20:20

How to pass columns to orm.update if the number of columns is not known in advance?

type My_table struct {
  Id int
  Name string
  Lastname string
}

o := orm.NewOrm()
field := My_table{Id: Id}
num, err := o.Update(&field, "name", "lastname")

There may be a situation where you need to update only one field, for example "name".
The function is created like this:
func (o *orm) Update(md interface{}, cols ...string) (int64, error)

It turns out that I need some kind of structure that would describe this cols ...string and I could add either one "name" field to it , or both "name", "lastname" .
Tell me, is it possible to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adel1ne, 2017-06-21
@Adel1ne

In general, everything turned out to be quite simple when you read the manuals carefully.
We make an array with the maximum number of columns that may need to be updated: And then, depending on the condition, we reassign a slice with the necessary elements to it: If we only need the "name" field: And we substitute this array into the update function, with three points, which denote a variable number of arguments:
num, err := o.Update(&device, str...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question