Skip to content

Instantly share code, notes, and snippets.

View mkock's full-sized avatar
💭
Currently building stuff in Go

Martin Kock mkock

💭
Currently building stuff in Go
View GitHub Profile
maybeUser, err := repo.LoadUser(42) // Returns interface{}, error
if err != nil { ... }
user, ok := maybeUser.(*User) // Ugly conversion
if !ok { ... }
var u User
repo := new(UserRepository)
err := repo.Load(42, &u)
if err != nil { ... }
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
type EntityLoader interface {
Load(id uint32, h Hydrater) error
}
type Hydrater interface {
Hydrate(database.Row) error
}
type EntityLoader interface {
LoadUser(id uint32) (User, error)
LoadAddress(id uint32) (Address, error)
LoadPurchaseOrder(id uint32) (PurchaseOrder, error)
}
type EntityLoader interface {
Load(id uint32) (interface{}, error)
}
// 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!
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
// School is defined in package school
type School interface {
Name() string
Location() string
students() []string
}
// University is defined in package schools
type University struct {}
// 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