Skip to content

Instantly share code, notes, and snippets.

@Guaderxx
Created December 15, 2023 11:16
Show Gist options
  • Save Guaderxx/7e45a67a1cde382855ad721d0c1ea9fd to your computer and use it in GitHub Desktop.
Save Guaderxx/7e45a67a1cde382855ad721d0c1ea9fd to your computer and use it in GitHub Desktop.
singleton
package main
import "fmt"
type SingletonCon struct {
url string
port string
}
var singletonConInstance *SingletonCon
func GetInstance() *SingletonCon {
if singletonConInstance == nil {
fmt.Println("Create singleton instance")
singletonConInstance = &SingletonCon{
url: "url",
port: "port",
}
} else {
fmt.Println("Singleton instance already created.")
}
return singletonConInstance
}
func main() {
for i := 0; i < 10; i++ {
GetInstance()
// This will executed 10 times
// use sync.Once | sync.OnceFunc | sync.Value
// only execute one time
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment