Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 540 Bytes

File metadata and controls

31 lines (22 loc) · 540 Bytes

Singleton

Singleton pattern provides single instance of object and ensures there is no second instance of the same. It is created on the request for instance, and same instance is served for every calls after that.

package pattern1

type single struct {
    item int
}

var instance *single // scope of the package.

func GetInstance() *single {
  if instance == nil {
    instance  = new (single)
  }
  return instance
}

func (s *single) Increment() int {
    s.item++
    return s.item
}


Home