go: 通过原子变量实现roundrobin切换 代码非常简单: ``` type RoundRobin struct{ index int64 } func (m *RoundRobin) GetIndex(max int) int64 { for { index := atomic.LoadInt64(&m.index) newIndex := index + 1 if int(newIndex) >= max { newIndex = 0 } if atomic.CompareAndSwapInt64(&m.index, index, newIndex) { return newIndex } else { continue } } } ``` 来自 大脸猪 写于 2022-05-23 14:52 -- 更新于2022-05-23 14:53 -- 0 条评论