refactor: stringer

This commit is contained in:
Inanc Gumus
2019-09-27 19:09:07 +03:00
parent 0127b45c1e
commit e6eba7314f
16 changed files with 130 additions and 65 deletions

View File

@@ -16,13 +16,15 @@ type book struct {
published timestamp
}
// book satisfies the fmt.Stringer
// String method makes the book an fmt.Stringer.
// The list was calling the embedded product type's String(),
// now it calls the book.String().
func (b *book) String() string {
// product.String() has a pointer receiver.
// That's why you need to manually take the product's address here.
// Therefore, you need to manually take the product's address.
//
// If you pass: "b.product", Go would pass it as a copy to Sprintf.
// If you pass: "b.product", Go would pass it as a copy to `Sprintf`.
// In that case, Go can't deference b.product automatically.
// It's because: b.product would be different valuea copy.
// It's because: b.product would be a different value, a copy.
return fmt.Sprintf("%s - (%s)", &b.product, b.published)
}