Skip to content

Commit b8a5c11

Browse files
authored
Merge pull request mouredev#5456 from kodenook/develop
Reto #23 - go
2 parents 8edb090 + 389a39a commit b8a5c11

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Roadmap/23 - SINGLETON/go/kodenook.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
)
7+
8+
type Singleton struct {
9+
data string
10+
}
11+
12+
var instancia *Singleton
13+
14+
var once sync.Once
15+
16+
func GetInstance() *Singleton {
17+
once.Do(func() {
18+
instancia = &Singleton{data: "Data example"}
19+
})
20+
return instancia
21+
}
22+
23+
func main() {
24+
25+
singleton := GetInstance()
26+
fmt.Println(singleton.data)
27+
28+
other := GetInstance()
29+
fmt.Println(other.data)
30+
31+
singleton.data = "New data"
32+
33+
fmt.Println(other.data)
34+
}

0 commit comments

Comments
 (0)