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
@mkock
mkock / example1.go
Created January 13, 2020 21:41
The Flyweight Pattern in Go, Example One
// Player represents a player in a game.
type Player struct {
ID uint32
Handle, Name, Country string
Games []string
}
var (
playerIDMap = make(map[uint32]Player)
playerHandleMap = make(map[string]uint32) // ref: playerIDMap
type foobar interface {
foo()
bar()
}
type itemA struct{}
func (a *itemA) foo() {
fmt.Println("foo on A")
}
type student interface {
register(context.Context, string, string, int)
enrollCourse(uint32, time.Time) error
pass(uint32) error
fail(uint32) error
}
type alsoStudent interface {
register(ctx context.Context, name, profession string, age int)
enrollCourse(courseID uint32, start time.Time) error
public interface Vehicle {
    public void start();
    public void stop();
}
public class Car implements Vehicle {
public void start() {
System.out.println("starting engine...");
}
public void stop() {
type Vehicle interface {
Start()
Stop()
}
type Car struct {}
func (c *Car) Start() {
fmt.Println("starting engine...")
}
type toaster interface {
toast()
}
type acmeToaster struct {}
func (a *acmeToaster) toast() { fmt.Println("Commencing toasting of bread...") }
func doToast(t toaster) {
t.toast()
// Verify that acmeToaster satisfies interface toaster.
var _ toaster = &acmeToaster{}
type empty interface {}
type monster struct {
damage int
}
func (m *monster) attack() int {
return m.damage
}
type attacker interface {
attack() int
type toaster interface {
toast()
}
type acmeToaster struct {}
func (a *acmeToaster) toast() { fmt.Println("Commencing toasting of bread...") }