diff --git a/Roadmap/23 - SINGLETON/go/kodenook.go b/Roadmap/23 - SINGLETON/go/kodenook.go new file mode 100644 index 0000000000..db41451c07 --- /dev/null +++ b/Roadmap/23 - SINGLETON/go/kodenook.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "sync" +) + +type Singleton struct { + data string +} + +var instancia *Singleton + +var once sync.Once + +func GetInstance() *Singleton { + once.Do(func() { + instancia = &Singleton{data: "Data example"} + }) + return instancia +} + +func main() { + + singleton := GetInstance() + fmt.Println(singleton.data) + + other := GetInstance() + fmt.Println(other.data) + + singleton.data = "New data" + + fmt.Println(other.data) +}