3 ms·
Enjoyed your article. I think that you are a little off the mark with respect to pointers though. The simple solution to your first example is to simply write
by fmstephe 14y ago
Enjoyed your article. I think that you are a little off the mark with respect to pointers though.
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 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 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/quadtree/quadtree.go https://github.com/fmstephe/location_server/blob/master/quad...
- pdw 14y agoYou don't need pointer-to-pointers to replace the entire struct, you can just do this: http://play.golang.org/p/2ZUvuyCtwr http://play.golang.org/p/2ZUvuyCtwr func main() { u := &User{Name: "Leto"} println(u.Name) Modify(u) println(u.Name) } func Modify(u *User) { *u = User{Name: "Paul"} }
- fmstephe 14y agoGood point. Thx :)
- Rygu 14y agoLooks like C pointer syntax!
- ithkuil 14y agoIndeed. They are pointers. You just cannot do pointer arithmetic on them. 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.