You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm pretty new to Go, so I may not be able to explain it right, but I've noticed this:
At some point in pointers-and-errors.md, you suggest to add some fmt.Printfs to observe that the addresses are different due to the fact that the wallet object is being copied. The calls look like this:
fmt.Printf("address of balance in Deposit is %v \n", &w.balance)
and they print something like this:
address of balance in Deposit is 0xc00000a640
I'm assuming this 0xc00000a640 is printed as Hex, because using &wallet.balance returns a pointer and that pointer is printed (when using %v) as Hex. I'm not sure (with my knowledge at this point) how Go knows it's a pointer (is there a type for pointers?) Anyway, so far so good.
However, later in the same doc, you suggest to implement a String() string method on the Balance type. Once I did that, the output of that fmt.Printf() changed to:
address of balance in Deposit is 0 BTC
Looks like, fmt.Printf() understands that &wallet.balance is now a pointer to an instance of Stringer interface, and uses the String() method to print the value of that pointer, instead of its address.
Two issues with that:
That fact has been left without explanation in pointers-and-errors.md
I tried to have both, the Hex output and the String() method, but the best I could do, is by replacing %v with %d in the fmt.Printf() call. But then I'm no longer getting the Hex output. How can I get the "natural" behaviour of fmt.Printf when passing a pointer to it, while also having a String() string implemented on the type?
The text was updated successfully, but these errors were encountered:
mkielar
changed the title
pointers-and-errors.md: fmt.Println before and after implementing String() on Balance
pointers-and-errors.md: fmt.Printf before and after implementing String() on Balance
Oct 25, 2023
Uhh, okay, I found #158, and the link to playground explains to use %p for explicitly formatting pointers, which worked. I still believe, however, that a word of explanation in the doc would be a good idea. Or maybe just use %p from the beginning and explain why?
I'm pretty new to Go, so I may not be able to explain it right, but I've noticed this:
At some point in
pointers-and-errors.md
, you suggest to add somefmt.Printf
s to observe that the addresses are different due to the fact that thewallet
object is being copied. The calls look like this:and they print something like this:
I'm assuming this
0xc00000a640
is printed as Hex, because using&wallet.balance
returns a pointer and that pointer is printed (when using%v
) as Hex. I'm not sure (with my knowledge at this point) how Go knows it's a pointer (is there a type for pointers?) Anyway, so far so good.However, later in the same doc, you suggest to implement a
String() string
method on theBalance
type. Once I did that, the output of thatfmt.Printf()
changed to:Looks like,
fmt.Printf()
understands that&wallet.balance
is now a pointer to an instance ofStringer
interface, and uses theString()
method to print the value of that pointer, instead of its address.Two issues with that:
pointers-and-errors.md
String()
method, but the best I could do, is by replacing%v
with%d
in thefmt.Printf()
call. But then I'm no longer getting the Hex output. How can I get the "natural" behaviour offmt.Printf
when passing a pointer to it, while also having aString() string
implemented on the type?The text was updated successfully, but these errors were encountered: