This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maybeUser, err := repo.LoadUser(42) // Returns interface{}, error | |
if err != nil { ... } | |
user, ok := maybeUser.(*User) // Ugly conversion | |
if !ok { ... } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var u User | |
repo := new(UserRepository) | |
err := repo.Load(42, &u) | |
if err != nil { ... } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type UserRepository struct { } | |
func (u *UserRepository) Load(id uint32, h Hydrater) error { | |
row := ... // Run a SELECT by id and retrieve the result in a database.Row. | |
return h.Hydrate(row) // h must be a pointer. | |
} | |
type User struct { | |
ID uint32 | |
Name string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type EntityLoader interface { | |
Load(id uint32, h Hydrater) error | |
} | |
type Hydrater interface { | |
Hydrate(database.Row) error | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type EntityLoader interface { | |
LoadUser(id uint32) (User, error) | |
LoadAddress(id uint32) (Address, error) | |
LoadPurchaseOrder(id uint32) (PurchaseOrder, error) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type EntityLoader interface { | |
Load(id uint32) (interface{}, error) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// University is defined in package schools | |
type University struct { | |
school.School | |
} | |
func (u University) Name() string { return "Copenhagen Highschool" } | |
func (u University) Location() string { return "Copenhagen City Center" } | |
// func (u University) students() []string { return []string{"George", "Ben", "Louise", "Calvin"} } // Can't redefine. | |
var _ school.School = University{} // Works! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cannot use University literal (type University) as type school.School in assignment: | |
University does not implement school.School (missing school.students method) | |
have students() []string | |
want school.students() []string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// School is defined in package school | |
type School interface { | |
Name() string | |
Location() string | |
students() []string | |
} | |
// University is defined in package schools | |
type University struct {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In zap/zapcore/core.go | |
type Core interface { | |
LevelEnabler | |
With([]Field) Core | |
Check(Entry, *CheckedEntry) *CheckedEntry | |
Write(Entry, []Field) error | |
Sync() error | |
} | |
// In zap/zapcore/hook.go |
NewerOlder