[go] all: replace explicit loops over maps with calls to maps package

3 views
Skip to first unread message

Kirill Kolyshkin (Gerrit)

unread,
Dec 17, 2025, 8:03:33 PM (2 days ago) Dec 17
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Kirill Kolyshkin has uploaded the change for review

Commit message

all: replace explicit loops over maps with calls to maps package

Replaces loops of the form

for k, v := range x { m[k] = v }

with a single call to a function from the maps package, added in
Go 1.23. Depending on the context, this could be maps.Copy,
maps.Insert, maps.Clone`, or maps.Collect.

Generated by

go fix -mapsloop ./...

using

go version go1.26-devel_ad85395442 Wed Dec 17 15:56:48 2025 -0800 linux/amd64
Change-Id: Icc50a749e7d65d185d0212824178fbf2d4afc139

Change diff

diff --git a/src/go/doc/reader.go b/src/go/doc/reader.go
index e84d6d9..2943c90 100644
--- a/src/go/doc/reader.go
+++ b/src/go/doc/reader.go
@@ -10,6 +10,7 @@
"go/ast"
"go/token"
"internal/lazyregexp"
+ "maps"
"path"
"slices"
"strconv"
@@ -792,11 +793,9 @@
// 1) move values
r.values = append(r.values, t.values...)
// 2) move factory functions
- for name, f := range t.funcs {
- // in a correct AST, package-level function names
- // are all different - no need to check for conflicts
- r.funcs[name] = f
- }
+ // in a correct AST, package-level function names
+ // are all different - no need to check for conflicts
+ maps.Copy(r.funcs, t.funcs)
// 3) move methods
if !predeclared {
for name, m := range t.methods {
diff --git a/src/internal/trace/summary.go b/src/internal/trace/summary.go
index 6609ed9..2baba52 100644
--- a/src/internal/trace/summary.go
+++ b/src/internal/trace/summary.go
@@ -6,6 +6,7 @@

import (
"cmp"
+ "maps"
"slices"
"strings"
"time"
@@ -170,13 +171,9 @@
func (s GoroutineExecStats) clone() (r GoroutineExecStats) {
r = s
r.BlockTimeByReason = make(map[string]time.Duration)
- for reason, dt := range s.BlockTimeByReason {
- r.BlockTimeByReason[reason] = dt
- }
+ maps.Copy(r.BlockTimeByReason, s.BlockTimeByReason)
r.RangeTime = make(map[string]time.Duration)
- for name, dt := range s.RangeTime {
- r.RangeTime[name] = dt
- }
+ maps.Copy(r.RangeTime, s.RangeTime)
return r
}

diff --git a/src/net/internal/socktest/switch.go b/src/net/internal/socktest/switch.go
index dea6d9288..a1c34eb 100644
--- a/src/net/internal/socktest/switch.go
+++ b/src/net/internal/socktest/switch.go
@@ -7,6 +7,7 @@

import (
"fmt"
+ "maps"
"sync"
)

@@ -45,9 +46,7 @@
func (sw *Switch) Sockets() Sockets {
sw.smu.RLock()
tab := make(Sockets, len(sw.sotab))
- for i, s := range sw.sotab {
- tab[i] = s
- }
+ maps.Copy(tab, sw.sotab)
sw.smu.RUnlock()
return tab
}
diff --git a/src/sync/atomic/example_test.go b/src/sync/atomic/example_test.go
index b378e30..9d21900 100644
--- a/src/sync/atomic/example_test.go
+++ b/src/sync/atomic/example_test.go
@@ -5,6 +5,7 @@
package atomic_test

import (
+ "maps"
"sync"
"sync/atomic"
"time"
@@ -63,9 +64,8 @@
defer mu.Unlock()
m1 := m.Load().(Map) // load current value of the data structure
m2 := make(Map) // create a new value
- for k, v := range m1 {
- m2[k] = v // copy all data from the current object to the new one
- }
+ // copy all data from the current object to the new one
+ maps.Copy(m2, m1)
m2[key] = val // do the update that we need
m.Store(m2) // atomically replace the current object with the new one
// At this point all new readers start working with the new version.
diff --git a/src/sync/map_reference_test.go b/src/sync/map_reference_test.go
index f98bb98..1909c7f 100644
--- a/src/sync/map_reference_test.go
+++ b/src/sync/map_reference_test.go
@@ -6,6 +6,7 @@

import (
isync "internal/sync"
+ "maps"
"sync"
"sync/atomic"
)
@@ -274,9 +275,7 @@
func (m *DeepCopyMap) dirty() map[any]any {
clean, _ := m.clean.Load().(map[any]any)
dirty := make(map[any]any, len(clean)+1)
- for k, v := range clean {
- dirty[k] = v
- }
+ maps.Copy(dirty, clean)
return dirty
}

diff --git a/src/text/template/funcs.go b/src/text/template/funcs.go
index ed9c7ea..33360ab 100644
--- a/src/text/template/funcs.go
+++ b/src/text/template/funcs.go
@@ -8,6 +8,7 @@
"errors"
"fmt"
"io"
+ "maps"
"net/url"
"reflect"
"strings"
@@ -90,9 +91,7 @@
// addFuncs adds to values the functions in funcs. It does no checking of the input -
// call addValueFuncs first.
func addFuncs(out, in FuncMap) {
- for name, fn := range in {
- out[name] = fn
- }
+ maps.Copy(out, in)
}

// goodFunc reports whether the function or method has the right result signature.

Change information

Files:
  • M src/go/doc/reader.go
  • M src/internal/trace/summary.go
  • M src/net/internal/socktest/switch.go
  • M src/sync/atomic/example_test.go
  • M src/sync/map_reference_test.go
  • M src/text/template/funcs.go
Change size: S
Delta: 6 files changed, 16 insertions(+), 23 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icc50a749e7d65d185d0212824178fbf2d4afc139
Gerrit-Change-Number: 730967
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Griesemer (Gerrit)

unread,
Dec 18, 2025, 12:47:19 PM (19 hours ago) Dec 18
to Kirill Kolyshkin, goph...@pubsubhelper.golang.org, Austin Clements, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Austin Clements, Ian Lance Taylor and Kirill Kolyshkin

Robert Griesemer voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Austin Clements
  • Ian Lance Taylor
  • Kirill Kolyshkin
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icc50a749e7d65d185d0212824178fbf2d4afc139
Gerrit-Change-Number: 730967
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Austin Clements <aus...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Robert Griesemer <g...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Austin Clements <aus...@google.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 17:47:14 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 3:44:26 PM (16 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Go LUCI, Robert Griesemer, Austin Clements, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Austin Clements, Ian Lance Taylor and Robert Griesemer

Kirill Kolyshkin voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Austin Clements
  • Ian Lance Taylor
  • Robert Griesemer
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icc50a749e7d65d185d0212824178fbf2d4afc139
Gerrit-Change-Number: 730967
Gerrit-PatchSet: 2
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Austin Clements <aus...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Robert Griesemer <g...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Robert Griesemer <g...@google.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Austin Clements <aus...@google.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 20:44:23 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 4:11:27 PM (15 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Go LUCI, Robert Griesemer, Austin Clements, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Austin Clements, Ian Lance Taylor and Robert Griesemer

Kirill Kolyshkin voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Austin Clements
  • Ian Lance Taylor
  • Robert Griesemer
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icc50a749e7d65d185d0212824178fbf2d4afc139
Gerrit-Change-Number: 730967
Gerrit-PatchSet: 3
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Austin Clements <aus...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Robert Griesemer <g...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Robert Griesemer <g...@google.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Austin Clements <aus...@google.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 21:11:23 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 8:06:37 PM (11 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Go LUCI, Robert Griesemer, Austin Clements, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Austin Clements, Ian Lance Taylor and Robert Griesemer

Kirill Kolyshkin voted and added 1 comment

Votes added by Kirill Kolyshkin

Hold+1

1 comment

Patchset-level comments
File-level comment, Patchset 3 (Latest):
Kirill Kolyshkin . resolved

Hold for Go 1.26 freeze.

Open in Gerrit

Related details

Attention is currently required from:
  • Austin Clements
  • Ian Lance Taylor
  • Robert Griesemer
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Holds
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Icc50a749e7d65d185d0212824178fbf2d4afc139
    Gerrit-Change-Number: 730967
    Gerrit-PatchSet: 3
    Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Reviewer: Austin Clements <aus...@google.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Reviewer: Robert Griesemer <g...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Robert Griesemer <g...@google.com>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Austin Clements <aus...@google.com>
    Gerrit-Comment-Date: Fri, 19 Dec 2025 01:06:33 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages