From b0918f796a4fbe8c197297c6c927796b5c557370 Mon Sep 17 00:00:00 2001 From: Tamer Tas Date: Tue, 19 Jan 2016 05:49:01 +0200 Subject: [PATCH] Implement singleton pattern --- singleton/singleton.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 singleton/singleton.go diff --git a/singleton/singleton.go b/singleton/singleton.go new file mode 100644 index 0000000..d85a4a6 --- /dev/null +++ b/singleton/singleton.go @@ -0,0 +1,20 @@ +package singleton + +import ( + "sync" +) + +type Object struct { +} + +var once sync.Once +var instance *Object + +func GetInstance() *Object { + // Creates a singleton instance once. + once.Do(func() { + instance = &singleton{} + }) + + return instance +}