Assigning struct values

142 views
Skip to first unread message

Mustafa Durukan

unread,
Sep 7, 2022, 2:28:43 PM9/7/22
to golang-nuts
fakeObject := tt.Object.(TypeOfStruct)
object := Object.(TypeOfStruct)

fakeObject.Field = object.Field
fakeObject.Field2 = object.Field2


I have case like that.
I want to assign sme object values to tt.Object but when i try to assign it assings to fakeObject i mean copy of tt.Object so I cant assign

I tried these assigns
tt.Object.(TypeOfStruct).Field = object.Field
tt.Object.(TypeOfStruct).Field2 = object.Field2


or

tt.Object:= tt.Object.(TypeOfStruct)
tt.Object.Field = object.Field
tt.Object.Field2 = object.Field2


But both of them didnt work
How should i achieve it?
btw tt.Object and Object are interface{} of course

burak serdar

unread,
Sep 7, 2022, 2:47:15 PM9/7/22
to Mustafa Durukan, golang-nuts
On Wed, Sep 7, 2022 at 12:28 PM Mustafa Durukan <mduru...@gmail.com> wrote:
fakeObject := tt.Object.(TypeOfStruct)
object := Object.(TypeOfStruct)

If you can include the definitions of tt, explain what you want to do, and what you mean by "didn't  work", someone may be able to answer this question. Without those, it is not clear what you are trying to achieve.
 

fakeObject.Field = object.Field
fakeObject.Field2 = object.Field2


I have case like that.
I want to assign sme object values to tt.Object but when i try to assign it assings to fakeObject i mean copy of tt.Object so I cant assign

I tried these assigns
tt.Object.(TypeOfStruct).Field = object.Field
tt.Object.(TypeOfStruct).Field2 = object.Field2


or

tt.Object:= tt.Object.(TypeOfStruct)
tt.Object.Field = object.Field
tt.Object.Field2 = object.Field2


But both of them didnt work
How should i achieve it?
btw tt.Object and Object are interface{} of course

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d1539b71-8344-4199-bb7c-ef95b3cf2badn%40googlegroups.com.

Martin Schnabel

unread,
Sep 8, 2022, 2:10:43 PM9/8/22
to Mustafa Durukan, golang-nuts
Hi Mustafa,

this can be easy if you put a pointer value of type *TypeOfStruct into
the interface. interface values of structs end up using pointers
underneath anyway so there is no performance reason not to.

The explanation is that if you type assert to TypeOfStruct, you copy
out that plain struct value and then operate on that copy.

If you store the pointer type value into tt.Object and type assert to
it, you copy out the pointer value and then operate on that pointer that
still points to the originally addressed struct value.

Hope that helps!

On 9/7/22 15:58, Mustafa Durukan wrote:
> *fakeObject := tt.Object.(TypeOfStruct)
> object := Object.(TypeOfStruct)
>
> fakeObject.Field = object.Field
> fakeObject.Field2 = object.Field2*
>
> I have case like that.
> I want to assign sme object values to tt.Object but when i try to assign
> it assings to fakeObject i mean copy of tt.Object so I cant assign
>
> I tried these assigns
> *tt.Object.(TypeOfStruct).Field = object.Field
> tt.Object.(TypeOfStruct).Field2 = object.Field2*
>
> or
>
> *tt.Object:= tt.Object.(TypeOfStruct)
> tt.Object.Field = object.Field
> tt.Object.Field2 = object.Field2*
>
> But both of them didnt work
> How should i achieve it?
> btw tt.Object and Object are interface{} of course
>
> --
> 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...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> <https://groups.google.com/d/msgid/golang-nuts/d1539b71-8344-4199-bb7c-ef95b3cf2badn%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages