Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointers-and-errors.md: fmt.Printf before and after implementing String() on Balance #711

Open
mkielar opened this issue Oct 25, 2023 · 2 comments · May be fixed by #767
Open

pointers-and-errors.md: fmt.Printf before and after implementing String() on Balance #711

mkielar opened this issue Oct 25, 2023 · 2 comments · May be fixed by #767

Comments

@mkielar
Copy link

mkielar commented Oct 25, 2023

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:

  1. That fact has been left without explanation in pointers-and-errors.md
  2. 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?
@mkielar 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
@mkielar
Copy link
Author

mkielar commented 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?

@mkielar
Copy link
Author

mkielar commented Oct 25, 2023

Huh, there's even a pull request for it: #616

@dario-piotrowicz dario-piotrowicz linked a pull request May 19, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant