func main() {
u := &User{Name: "Leto"}
Modify(&u)
}
func Modify(u **User) {
u = &User{Name: "Paul"}
}
With no luck. func main() {
u := &User{Name: "Leto"}
Modify(&u)
}
func Modify(u **User) {
u = &User{Name: "Paul"}
}
With no luck.The simple solution to your first example is to simply write to the Name field of the struct being pointed too.
http://play.golang.org/p/D3t0Iv8oG4
If you want to do complex pointer meddling (and replace the entire struct) you need this syntax.
http://play.golang.org/p/tMwHC-k-bu
As a side note I have personally used pointers to interfaces for doing search tree reordering. You can really do a lot of useful pointer operations without pointer arithmetic.
(A link to a quadtree implementation which uses pointers to interfaces, on line 119) https://github.com/fmstephe/location_server/blob/master/quad...
func main() {
u := &User{Name: "Leto"}
println(u.Name)
Modify(u)
println(u.Name)
}
func Modify(u *User) {
*u = User{Name: "Paul"}
}In declaration the '' goes on the left though. In general Go's syntax inverts the declaration w.r.t C/C++:
a int vs int a
b *int vs int* b
type X int vs typedef int X
NOTE: you can do unsafe pointer arithmetic using the "unsafe" package.
You can convert any pointer to a unsafe.Pointer and then you can convert it to a uintptr.There are also library functions designed to work with that, for example atomic.AddUintptr that atomically adds an offset to a pointer.
Although not intended as general use, it can be useful in low level library code. Otherwise you can do pretty much everything using slices.
If you want to cause side effects in a function, you have to dereference something. (or call another evil function)
The only exception being dot operators, which implicitly dereference their left hand argument.
(&x).whatever() == x.whatever()
I wish go had used arrow notation now. Explicitly marking side effects is a good thing.For example if you pass a struct, you are passing it by value, but if you pass a pointer to that struct, you are also passing by the argument by value. Why? Because even though the pointer contains a reference to the struct as its value, the pointer itself (which is the same as any other integer datatype to the computer) is being passed by value.
If your not familiar with pointers and C and languages with reference values like C++/Java this distinction isn't going to mean much to you however.
http://msdn.microsoft.com/en-gb/library/14akc2c7(v=vs.71).as...
I would expect that refs are roughly equivalent to the following
http://play.golang.org/p/3cwx5Qofc8
Where the commented out code represents the .net equivalent. I would expect that the .net compiler simply rewrites your code for you to hide the slightly tricky messing about with pointers. So the .net ref looks like a bit of syntactic sugar over what you were trying to do in your post.
I don't know about you, but I understand pointers from C, and then learning assembly. Go's behavior isn't different from C here.
Also, elsewhere you are discussing pass by value/pass by reference. Technically, C/Go/Java are pass by value. Passing pointers and de-referencing it is how pass-by-ref is emulated. You will notice that the top comment has qualified pass-by-reference with "passing pointers". That is colloquial terminology.
What are you trying to do, and how would you think it works in your examples? I assume you're trying to change the name of the object being pointed to, but you're just assigning a new value to a variable that is passed to a function. Therefore, when the function returns, that value is 'lost'. How could it ever change the value of a member of the object you're pointing to? I have never written a line of Go in my life, but with the knowledge that Go supports some form of pointers, it's quite easy to trace this back to how it would work in C or C++; it also wouldn't work there, either. Knowing what a pointer is is easy, it's just memorizing a definition - knowing how they work takes some experience.
func main() {
u := User{Name: "Leto"}
Modify(&u)
}
func Modify(u *User) {
// u = &User{Name: "Paul"}
// you could, but why?
u.Name = "Paul"
}
I mean, if you read your code for main, you're saying "Declare u to be a pointer to a new User. Then call modify with the address of u... aka, the address of the pointer that points to the actual User object"...This is pretty much basic pointer stuff, the same as you would do in C: runnable: http://codepad.org/TeUZi1Wp
RE: "Go is pass by reference": I don't see anyone claiming that. The top comment is trying to be nice by saying that you can emulate pass by reference by passing a pointer (but you're really just passing the pointer by value).
I mean, what do you think this Java code prints (objects are references, but passed by val):
Runnable: http://rextester.com/JBZCLG13581
You should also consider the good comment above displaying how you actually CAN do REAL swapping in Go: http://news.ycombinator.com/item?id=5357959 (try that in Java... heh)
When I started learning Go, I kept seeing people talk about pass by reference, so I assumed it really did pass by reference (despite being uncommon). I was surprised to find out that it does pass by value.
You're correct about the "ref" keyword in C#, when used for the arg and parameter, it causes the function or method to act on the same chuck of memory as the alias represented in the caller.