企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## once 通过once实现单例模式: ~~~ type Singleton struct { } var instance *Singleton var once sync.Once func GetInstance() *Singleton { once.Do(func() { fmt.Println("初始化Singleton") instance = new(Singleton) }) return instance } func TestSingletonExample1(t *testing.T) { var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) go func() { obj := GetInstance() fmt.Printf("instance address:%p \n", unsafe.Pointer(obj)) wg.Done() }() } wg.Wait() fmt.Println("main goroutine end") } ~~~