Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 595 Bytes

README.md

File metadata and controls

35 lines (27 loc) · 595 Bytes

Usage

the singleton pattern is a software design pattern that restricts the instantiation of a type to one object

Example

package main 

import (
	"github.com/donutloop/toolkit/singleton"
)

type config struct {
	Addr string
	Port int
}

var configSingleton singleton.NewSingleton = singleton.NewSingleton(func() (interface{}, error) {
	return &config{Addr:"localhost", Port:80}, nil
})

func Config() (*config, error) {
	s, err := configSingleton.Get()
	if err != nil {
		return nil, err
	}
	return s.(*config), nil
}

func main() {
	config, err := Config()
	// do things 
}