FAQ

On Fri, Sep 4, 2015 at 12:55 PM, Aaron Wood wrote:
You say that I am causing a leak when calling Ping() because it calls
d.Connect(). Is that because I'm calling a method that takes a pointer
receiver from a method that doesn't or just because I'm overwriting the
handle each time? Either way, I should really change it as it's poor design
on my part :)
It's because if you say (ignoring error handling):

d := NewD()
d.Ping()
println(d.Handle)

then you'll print nil, even though Ping calls Connect which sets the
Handle, because the receiver in "d.Ping()" is a copy of the entire d
struct, and anything that happens inside Ping only modifies that copy.
Once Ping returns, nothing in the d variable in this code fragment has
changed. If you called d.Ping a second time, you'd Connect with a
second copy, and again drop the resultant Handle.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 8 of 13 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 4, '15 at 2:12a
activeSep 5, '15 at 12:23a
posts13
users7
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase