Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy pointer values *a = *b in golang

Tags:

go

type T struct {     Id int     Name string }  func Copy(a *T, b *T) error {     b.Id=5     b.Name="gert"     a = b     return nil } 

a is still empty, I have to do it like this

func Copy(a *T, b *T) error {     b.Id = 5     b.Name = "gert"     a.Id = b.Id     a.Name = b.Name     return nil } 

now a is the same as b

Why and how can I copy *b to *a directly?

like image 554
Gert Cuykens Avatar asked Jan 09 '14 03:01

Gert Cuykens


People also ask

What does * and & indicate in Golang?

1. simply saying & will point to variable that has no value while * will point to variable that has value in there.

How do you copy variables in Golang?

A struct variable in Golang can be copied to another variable easily using the assignment statement(=). Any changes made to the second struct will not be reflected back to the first struct.

How do you copy an object in Golang?

Golang provides no builtin deep copy functionality so you'll have to implement your own or use one of the many freely available libraries that provide it. Show activity on this post. DeepCopy is a very heavy operation and hence should be avoided if possible.

Are pointers copied?

One function passes a pointer to the value of an object to another function. Both functions can access the value of that object, but the value itself is not copied.


2 Answers

Your first example is almost right. You pass in pointers to two objects. You put those pointers into variables A and B. But A and B are local variables, so when you say a=b you are merely saying "forget what was in A (locally)". The rest of the program still has pointers to those two original objects.

If you want to copy the data structure at B into the data structure at A, do this instead:

*a = *b; 

As dmikalova pointed out in the comments below, this merely copies the structs -- but not any data the struct points to. If your struct has a pointer, the data it points to is now shared by the two copies (because it only copied the pointer).

Technically, strings are always pointers, so they are never copied as part of your struct. But because strings are immutable (and Go has Garbage Collection), strings "feel" like they are part of your struct, and you don't have to worry about the low-level string sharing that magically saves you memory without you having to think about it.

like image 55
BraveNewCurrency Avatar answered Sep 25 '22 08:09

BraveNewCurrency


Generic solution for framework developers :

func Copy(source interface{}, destin interface{}) {     x := reflect.ValueOf(source)     if x.Kind() == reflect.Ptr {         starX := x.Elem()         y := reflect.New(starX.Type())         starY := y.Elem()         starY.Set(starX)         reflect.ValueOf(destin).Elem().Set(y.Elem())     } else {         destin = x.Interface()     } } 

So:

Copy(old, new) 
see this code in play.golang.org So you can pass any type at run time as long as you're sure that source and destin are both of the same type, (and destin is a pointer to that type).
like image 30
Mohsen Avatar answered Sep 24 '22 08:09

Mohsen