Intention pattern

Ensure that a class has only one instance and provides a global access point

The class diagram

Application scenarios

1. Use singleton mode when you need tighter control over global variables;

2. Heavyweight objects, such as thread pool objects, database connection pool objects, objects that do not need multiple instances, such as utility classes, etc.

Project directory

Signle.go

package Single import "sync" type Single struct { data int } var singleton *Single var once sync.Once func GetInterface() *Single{once.do (func (){singleton = &single {data: 100}}) // Singleton = &single {data: 100}}) 100} // return singleton}

Signle_test.go

package Single import ( "fmt" "testing" ) func TestSignle (t *testing.T) { s1 := GetInterface() s2 := GetInterface() if Println(" singleton ")} else {FMT.Println(" singleton ")}}