Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created April 13, 2014 21:00
Show Gist options
  • Save vaskoz/10602142 to your computer and use it in GitHub Desktop.
Save vaskoz/10602142 to your computer and use it in GitHub Desktop.
Singleton pattern in Golang
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