Skip to content

Instantly share code, notes, and snippets.

View mkock's full-sized avatar
💭
Currently building stuff in Go and Vue.js

Martin Kock mkock

💭
Currently building stuff in Go and Vue.js
View GitHub Profile
@mkock
mkock / example1_3.go
Last active January 19, 2020 06:15
Three data structures containing the same Players
var (
playerIDMap = make(map[uint32]Player)
playerHandleMap = make(map[string]Player)
playerCountryMap = make(map[string][]Player)
)
@mkock
mkock / example1_4.go
Last active January 19, 2020 06:16
Lookup functions before refactoring
// FindPlayerByID returns the player with the given ID, if exists.
// Otherwise, an empty Player is returned.
func FindPlayerByID(ID uint32) Player {
if p, ok := playerIDMap[ID]; ok {
return p
}
return Player{}
}
// FindPlayerByHandle returns the player with the given Handle,
@mkock
mkock / example1_5.go
Last active January 19, 2020 06:17
Refactored data structures
var (
playerIDMap = make(map[uint32]Player)
playerHandleMap = make(map[string]uint32) // ref: playerIDMap
playerCountryMap = make(map[string][]uint32) // ref: playerIDMap
)
@mkock
mkock / example1_6.go
Last active January 19, 2020 06:18
Refactored lookup functions
// FindPlayerByID returns the player with the given ID, if exists.
// Otherwise, an empty Player is returned.
func FindPlayerByID(ID uint32) Player {
if p, ok := playerIDMap[ID]; ok {
return p
}
return Player{}
}
// FindPlayerByHandle returns the player with the given Handle,
@mkock
mkock / example2_1.go
Last active January 19, 2020 06:12
Definition of cachedPlayer
// cachedPlayer is a Player, but without the Games field.
type cachedPlayer struct {
ID uint32
Handle, Name, Country string
}
@mkock
mkock / example2_2.go
Last active January 19, 2020 06:11
Cache and games list
var games = []string{"Flappy Fish", "Ducks'n'Dogs", "Backflip Pro"}
var crashyGamesPlayers map[uint32]cachedPlayer
@mkock
mkock / example2_3.go
Last active January 19, 2020 06:10
Converter method for cachedPlayer
// convertWith returns the Player that matches cachedPlayer,
// with game titles attached.
func (c cachedPlayer) convertWith(games []string) Player {
return Player{
ID: c.ID,
Handle: c.Handle,
Name: c.Name,
Country: c.Country,
Games: games,
}
@mkock
mkock / example2_4.go
Last active January 19, 2020 06:08
Implementation of FindPlayerByID
// FindPlayerByID returns the player with the given ID, if exists.
// Otherwise, an empty Player is returned.
func FindPlayerByID(ID uint32) Player {
if cp, ok := crashyGamesPlayers[ID]; ok {
// cachedPlayer found.
return cp.convertWith(games) // Using globally cached games.
}
return Player{}
}
@mkock
mkock / keybaseproof.md
Created May 27, 2020 12:13
Keybase proof

Keybase proof

I hereby claim:

  • I am mkock on github.
  • I am mkock (https://keybase.io/mkock) on keybase.
  • I have a public key ASAZA0SygMgNgJLx_fZhwtso0TiCl2sLVre8QaPxH5U0owo

To claim this, I am signing this object:

@mkock
mkock / busride_code_snippet_01.go
Created August 20, 2020 18:46
Bus Ride, code snippet 01
package busservice
// Passenger represents a bus passenger, uniquely identified by their SSN.
type Passenger struct {
SSN string
SeatNumber uint8
}
// Bus carries Passengers from A to B if they have a valid bus ticket.
type Bus struct {