在這樣的結構中使用切片可以嗎:type buffer struct { values []int mutex sync.RWMutex}我問,因為當我們追加切片時,我們有可能將切片復制到內存中的新位置。
1 回答

白板的微信
TA貢獻1883條經驗 獲得超3個贊
在大多數Go編程中,人們會在沒有性能問題的情況下分配切片,以防返回新內存,因為切片是參考值。append
b.values = append(b.values, i)
type buffer struct {
values []int
mutex sync.RWMutex
}
func (b *buffer) Append(i int) {
b.mutex.Lock()
b.values = append(b.values, i)
b.mutex.Unlock()
}
- 1 回答
- 0 關注
- 89 瀏覽
添加回答
舉報
0/150
提交
取消