That's correct. Method receivers are equivalent to just passing as an
extra argument:
func Set(f fetcher, tname string) {
f.names = tname
}
In this case, it's obvious that Set will not affect the fetcher passed in
at the call site, in contrast to
func Set(f *fetcher, tname string) {
f.names = tname
}
...which would have the desired effect. Same with pointer receivers:
func (f fetcher) Set(tname string) {
f.names = tname
}
vs
func (f *fetcher) Set(tname string) {
f.names = tname
}
- Augusto
On Tuesday, September 13, 2016 at 12:14:29 PM UTC-7, Frank Davidson wrote:
>
> Thanks, guys!
>
> So, just for me to be clear, the line me.Set("It works!") in my original
> code created a copy of the "me" fetcher, set its names field, and then
> threw it away, leaving the original "me.name" with an unaltered, empty
> string value?
>
>
> On Tuesday, September 13, 2016 at 2:41:46 PM UTC-4, Frank Davidson wrote:
>>
>> I think I'm having some type of mental lapse, but can anyone tell me why
>> this doesn't print out "It works!" nor give me an error? It simply prints
>> an empty string...
>>
>> https://play.golang.org/p/9oG0S8kfM3
>>
>> package main
>>
>> import "fmt"
>>
>> type fetcher struct { names string }
>>
>> func (f fetcher) Set(tname string) {
>> f.names = tname
>> }
>>
>> func main() {
>> var me fetcher
>> me.Set("It works!")
>> fmt.Println(me.names)
>> }
>>
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.