Created
January 27, 2023 10:06
-
-
Save arriqaaq/7c97d0be96b9487f6230df940e9a12e8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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