个人经验来参考: * 因为 Go 的常驻内存,一种是初始化启动、还一种是按需启动。 * 常驻内存下,多个依赖的服务,其实希望的是 `*LeakyLimiter` 是一致的,如果改成对象调整,当 A、B 两个依赖这个服务的指针会因为内部变化而影响另外一个。 * * * **关于第二点的补充:** 比如,`c2` 和 `c1` 因为调用的是同一个 `l`,因为 `l.WithCount(3)` 导致 `l` 会互相影响 package main import "fmt" func main() { l := NewLimiter(5) c1 := NewCase(l) c2 := NewCase(l.WithCount(3)) fmt.Println(c1, c2) } type Case struct { limiter *Limiter } func NewCase(limiter *Limiter) *Case { return &Case{ limiter: limiter, } } func (c *Case) Allow() bool { return c.limiter.Allow() } type Limiter struct { Count int } func NewLimiter(count int) *Limiter { return &Limiter{ Count: count, } } func (l *Limiter) WithCount(c int) *Limiter { l.Count = c return l } func (l *Limiter) Allow() bool { return l.Count > 0 }