Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

单例设计模式Singleton 是否应该改为包私有的,否者调用者也可以直接调用Singleton实例会对象。 #15

Closed
ruster-cn opened this issue Sep 11, 2020 · 1 comment

Comments

@ruster-cn
Copy link

package singleton

import "sync"

//Singleton 是单例模式类
type Singleton struct{}

var singleton *Singleton
var once sync.Once

//GetInstance 用于获取单例模式对象
func GetInstance() *Singleton {
	once.Do(func() {
		singleton = &Singleton{}
	})

	return singleton
}

=>

package singleton

import "sync"

//Singleton 是单例模式类
type singleton struct{}

var singleton *singleton
var once sync.Once

//GetInstance 用于获取单例模式对象
func GetInstance() *singleton {
	once.Do(func() {
		singleton = &singleton{}
	})

	return singleton
}
@senghoo
Copy link
Owner

senghoo commented Nov 3, 2020

Good Job。如果可以的话,麻烦提个PR。直接做一下合并。

@senghoo senghoo closed this as completed Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants