@@ -18,6 +18,7 @@ package featuregate
1818import (
1919 "flag"
2020 "fmt"
21+ "maps"
2122 "sort"
2223 "strconv"
2324 "strings"
@@ -171,10 +172,7 @@ func New(name string, lg *zap.Logger) *featureGate {
171172 if lg == nil {
172173 lg = zap .NewNop ()
173174 }
174- known := map [Feature ]FeatureSpec {}
175- for k , v := range defaultFeatures {
176- known [k ] = v
177- }
175+ known := maps .Clone (defaultFeatures )
178176
179177 f := & featureGate {
180178 lg : lg ,
@@ -217,13 +215,9 @@ func (f *featureGate) SetFromMap(m map[string]bool) error {
217215
218216 // Copy existing state
219217 known := map [Feature ]FeatureSpec {}
220- for k , v := range f .known .Load ().(map [Feature ]FeatureSpec ) {
221- known [k ] = v
222- }
218+ maps .Copy (known , f .known .Load ().(map [Feature ]FeatureSpec ))
223219 enabled := map [Feature ]bool {}
224- for k , v := range f .enabled .Load ().(map [Feature ]bool ) {
225- enabled [k ] = v
226- }
220+ maps .Copy (enabled , f .enabled .Load ().(map [Feature ]bool ))
227221
228222 for k , v := range m {
229223 k := Feature (k )
@@ -280,9 +274,7 @@ func (f *featureGate) Add(features map[Feature]FeatureSpec) error {
280274
281275 // Copy existing state
282276 known := map [Feature ]FeatureSpec {}
283- for k , v := range f .known .Load ().(map [Feature ]FeatureSpec ) {
284- known [k ] = v
285- }
277+ maps .Copy (known , f .known .Load ().(map [Feature ]FeatureSpec ))
286278
287279 for name , spec := range features {
288280 if existingSpec , found := known [name ]; found {
@@ -336,9 +328,7 @@ func (f *featureGate) OverrideDefault(name Feature, override bool) error {
336328// GetAll returns a copy of the map of known feature names to feature specs.
337329func (f * featureGate ) GetAll () map [Feature ]FeatureSpec {
338330 retval := map [Feature ]FeatureSpec {}
339- for k , v := range f .known .Load ().(map [Feature ]FeatureSpec ) {
340- retval [k ] = v
341- }
331+ maps .Copy (retval , f .known .Load ().(map [Feature ]FeatureSpec ))
342332 return retval
343333}
344334
@@ -397,13 +387,9 @@ func (f *featureGate) KnownFeatures() []string {
397387func (f * featureGate ) DeepCopy () MutableFeatureGate {
398388 // Copy existing state.
399389 known := map [Feature ]FeatureSpec {}
400- for k , v := range f .known .Load ().(map [Feature ]FeatureSpec ) {
401- known [k ] = v
402- }
390+ maps .Copy (known , f .known .Load ().(map [Feature ]FeatureSpec ))
403391 enabled := map [Feature ]bool {}
404- for k , v := range f .enabled .Load ().(map [Feature ]bool ) {
405- enabled [k ] = v
406- }
392+ maps .Copy (enabled , f .enabled .Load ().(map [Feature ]bool ))
407393
408394 // Construct a new featureGate around the copied state.
409395 // Note that specialFeatures is treated as immutable by convention,
0 commit comments