Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 120
Good morning! Here's your coding interview problem for today.

This problem was asked by Microsoft.

Implement the singleton pattern with a twist. First, instead of storing one instance, store two instances. And in every even call of `getInstance()`, return the first instance and in every odd call of `getInstance()`, return the second instance.

Pull request description:
Day120
Resolves #248.

Patch:
diff --git a/README.md b/README.md
index ba424c0..a27ef47 100644
--- a/README.md
+++ b/README.md
@@ -126,3 +126,4 @@ problems from
 * [Day 114](https://github.com/vaskoz/dailycodingproblem-go/issues/237)
 * [Day 115](https://github.com/vaskoz/dailycodingproblem-go/issues/238)
 * [Day 116](https://github.com/vaskoz/dailycodingproblem-go/issues/242)
+* [Day 120](https://github.com/vaskoz/dailycodingproblem-go/issues/248)
diff --git a/day120/problem.go b/day120/problem.go
new file mode 100644
index 0000000..114abde
--- /dev/null
+++ b/day120/problem.go
@@ -0,0 +1,32 @@
+package day120
+
+// singleton represents any singleton object.
+type singleton struct {
+	t int
+}
+
+// global contains only number of call state.
+type global struct {
+	evenOrOdd int
+	even, odd *singleton
+}
+
+// Global represents a global context in which singletons exist.
+type Global interface {
+	GetInstance() interface{}
+}
+
+// New returns a new Global context.
+func New() Global {
+	return &global{evenOrOdd: 0, even: &singleton{0}, odd: &singleton{1}}
+}
+
+// GetInstance returns one of two possible singletons.
+// The returned singleton alternates between calls.
+func (g *global) GetInstance() interface{} {
+	g.evenOrOdd = (g.evenOrOdd + 1) % 2
+	if g.evenOrOdd == 0 {
+		return g.even
+	}
+	return g.odd
+}


Test patch:
diff --git a/day120/problem_test.go b/day120/problem_test.go
new file mode 100644
index 0000000..8f9961e
--- /dev/null
+++ b/day120/problem_test.go
@@ -0,0 +1,27 @@
+package day120
+
+import "testing"
+
+func TestGetInstance(t *testing.T) {
+	g := New()
+	first := g.GetInstance()
+	second := g.GetInstance()
+	if first == second {
+		t.Errorf("The first and second instance should not be the same")
+	}
+	for i := 0; i < 100; i++ {
+		if first != g.GetInstance() {
+			t.Errorf("First doesn't match")
+		}
+		if second != g.GetInstance() {
+			t.Errorf("Second doesn't match")
+		}
+	}
+}
+
+func BenchmarkGetInstance(b *testing.B) {
+	g := New()
+	for i := 0; i < b.N; i++ {
+		g.GetInstance()
+	}
+}