Example analysis of the go language singleton pattern of Singleton


This article illustrates the use of the go language singleton pattern (Singleton). Share with you for your reference. The specific analysis is as follows:

Singleton pattern (Singleton) : indicates that a class will only generate 1 object with only 1. The singleton pattern has the following properties: A. These classes can only have one instance; B. These can be instantiated automatically; C. This class is visible to the entire system, that is, the instance must be provided to the entire system.

package singleton
import "fmt"
var _instance *object
type object struct {
    name string
}
func Instance() *object {
   if _instance == nil {
       _instance = new(object)
   }
   return _instance
}
func (p *object) Setname(name string) {
    p.name = name
}
func (p *object) Say() {
    fmt.Println(p.name)
}

I hope this article has been helpful to your programming of Go language.