Go语言实现单例

2025-3-10 diaba 基础

type Example struct{}

var doOnce sync.Once
var INSTANCE *Example

func NewExample() *Example {
	doOnce.Do(func() {
		INSTANCE = &Example{}
	})
	return INSTANCE
}

发表评论:

Powered by emlog 京ICP备15045175号-1 Copyright © 2022