Skip to content

Instantly share code, notes, and snippets.

@arriqaaq
Created January 27, 2023 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arriqaaq/7c97d0be96b9487f6230df940e9a12e8 to your computer and use it in GitHub Desktop.
Save arriqaaq/7c97d0be96b9487f6230df940e9a12e8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
)
type Character struct {
Appearance string
Name string
}
var characterPool = sync.Map{}
func NewCharacter(appearance, name string) *Character {
if character, ok := characterPool.Load(appearance); ok {
return character.(*Character)
}
newCharacter := &Character{appearance, name}
characterPool.Store(appearance, newCharacter)
return newCharacter
}
func main() {
character1 := NewCharacter("Soldier", "John")
character2 := NewCharacter("Soldier", "Mike")
character3 := NewCharacter("Zombie", "Zombie1")
fmt.Printf("character1: %p %+v\n", character1, character1)
fmt.Printf("character2: %p %+v\n", character2, character2)
fmt.Printf("character3: %p %+v\n", character3, character3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment