Skip to content

Commit

Permalink
Reto #23 - go
Browse files Browse the repository at this point in the history
  • Loading branch information
kodenook-dev committed Aug 10, 2024
1 parent 6f038fd commit 389a39a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Roadmap/23 - SINGLETON/go/kodenook.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 389a39a

Please sign in to comment.