On 2 August 2016 at 09:58, Rayland <guianul...@gmail.com> wrote:
> I am working in the context of an ODM.
>
> I have an update method, like this:
>
> func Update(key string, valuePtr interface{}) error
>
>
> Inside of this method I need to do a Get in order to get the old value,
> compare the modified fields and update them, then insert the new object in
> the database.
>
> The problem is that inside the Update method I don't know what type valuePtr
> is in order to make a second object and feed to the Get method, and then if
> I call the Get method with an interface{} object it will crash because it
> can't Unmarshal (objects are kept as json in the database).

FWIW Axel's solution can work well and I've use similar techniques in places,
but I have sympathy for Jan's reservations. Why do you need to do a Get
before doing the Put - how can you tell which fields need to be updated?
I can't immediately see how what you suggest would be different from
just updating the database with the value directly.

  cheers,
    rog.

>
> On Tuesday, August 2, 2016 at 11:26:01 AM UTC+3, Dave Cheney wrote:
>>
>> Hypothetically, if there was a syntax in Go that did this, what would you
>> do next ? In other words, what is the problem you are trying to solve that
>> has lead you to needing this feature?
>>
>> On Tuesday, 2 August 2016 18:18:05 UTC+10, Rayland wrote:
>>>
>>> I basically need something like this:
>>>
>>> var obj = &Person{Name: "John"} // I don't know what type obj will be
>>> everytime
>>> var newObj interface{}
>>>
>>> initNewObj(obj) // now newObj is an empty object of type *Person
>>>
>>>
>>>
>>>
>>> On Tuesday, August 2, 2016 at 10:21:01 AM UTC+3, Dave Cheney wrote:
>>>>
>>>> TL;DR a shallow copy is easy, but it's almost never what you want.
>>>>
>>>> https://play.golang.org/p/wpWN3Znop8
>>>>
>>>> Needing to copy an arbitrary value is usually an anti pattern as values
>>>> can contain references to other values, which contain references to other
>>>> values, which contain references to other values, sometimes leading back to
>>>> the previous values.
>>>>
>>>> On Tuesday, 2 August 2016 17:13:21 UTC+10, Rayland wrote:
>>>>>
>>>>> Let's say I have an object A of some type. Is there a way to create a
>>>>> new object B with the same type as A without knowing the type of A in
>>>>> advance?
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to