We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 173ce4e commit fc6b5dfCopy full SHA for fc6b5df
03_singleton/singleton.go
@@ -0,0 +1,20 @@
1
+package singleton
2
+
3
+import "sync"
4
5
+//Singleton 是单例模式类
6
+type Singleton struct{}
7
8
+var singletonInst *Singleton
9
+var singletonInstLock sync.Mutex
10
11
+//GetInstance 用于获取单例模式对象
12
+func GetInstance() *Singleton {
13
+ if singletonInst == nil {
14
+ singletonInstLock.Lock()
15
16
+ singletonInst = &Singleton{}
17
+ }
18
19
+ return singletonInst
20
+}
03_singleton/singleton_test.go
@@ -0,0 +1,11 @@
+import "testing"
+func TestSingleton(t *testing.T) {
+ ins1 := GetInstance()
+ ins2 := GetInstance()
+ if ins1 != ins2 {
+ t.Fatal("instance is not equal")
0 commit comments