Created
April 13, 2014 21:00
-
-
Save vaskoz/10602142 to your computer and use it in GitHub Desktop.
Singleton pattern in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
var instance TheOnlyOne = &onlyOne{time.Now()} | |
type TheOnlyOne interface { | |
} | |
type onlyOne struct { | |
created_at time.Time | |
} | |
func New() TheOnlyOne { | |
return instance | |
} | |
func main() { | |
a := New() | |
fmt.Printf("%T -> %p\n", a, a) | |
b := New() | |
fmt.Printf("%T -> %p\n", b, b) | |
if a == b { | |
fmt.Println("Yep this is the very same reference") | |
} else { | |
fmt.Println("Well this is a fine mess") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment