This question already has an answer here:
All:
Just one quick question:
In Tour of Go tutorial
https://tour.golang.org/methods/18
I tried to implement that String()
method as
func (ip *IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}
I used a pointer receiver but when it does not work in main()
for n, a := range addrs {
fmt.Printf("%v: %v
", n, a)
}
still show [127, 0, 0, 1]
It only works when I change it to value receiver like:
func (ip IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}
Does it mean fmt only looking for value receiver version of String()
or did I miss something for my implementation?
Thanks
</div>
Does Stringers interface only work with value receiver in golang ?
No.