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结构体字段为空,多次赋值instance值是一样的 #30

Open
joengtou opened this issue Jan 29, 2024 · 0 comments

Comments

@joengtou
Copy link

如下代码验证了空结构体多次赋值结果一样,在单例模式中golang-design-pattern/03_singleton/singleton.go line23,即使不用sync.Once 也能通过单元测试,应该给singleton添加一个字段

`package main

import (
"fmt"
)

var a1 *singleton1
var a2 *singleton2

type singleton1 struct {
}
type singleton2 struct {
d int
}

func getInstance1() *singleton1 {
a1 = &singleton1{}
return a1
}
func getInstance2() *singleton2 {
a2 = &singleton2{}
return a2
}
func main() {
b1, c1 := getInstance1(), getInstance1()
fmt.Println(b1 == c1)
//true
b2, c2 := getInstance2(), getInstance2()
fmt.Println(b2 == c2)
//false
}
`

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

1 participant