[go] all: replace HasPrefix/TrimPrefix with CutPrefix

3 views
Skip to first unread message

Kirill Kolyshkin (Gerrit)

unread,
Dec 17, 2025, 8:03:31 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 HasPrefix/TrimPrefix with CutPrefix

Simplify a common pattern where code first checks for a prefix with
strings.HasPrefix and then removes it with strings.TrimPrefix. This
is replaced with a single call to strings.CutPrefix, added in Go 1.20.

Generated by

go fix -stringscutprefix ./...

using

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

Change diff

diff --git a/src/html/template/exec_test.go b/src/html/template/exec_test.go
index 5a341b8..f6a03e4 100644
--- a/src/html/template/exec_test.go
+++ b/src/html/template/exec_test.go
@@ -1599,8 +1599,8 @@
"Nil": nil,
"Zero": 0,
})
- if strings.HasPrefix(tt.out, "ERROR:") {
- e := strings.TrimSpace(strings.TrimPrefix(tt.out, "ERROR:"))
+ if after, ok := strings.CutPrefix(tt.out, "ERROR:"); ok {
+ e := strings.TrimSpace(after)
if err == nil || !strings.Contains(err.Error(), e) {
t.Errorf("%s: Execute: %v, want error %q", tt.text, err, e)
}
diff --git a/src/internal/fuzz/pcg.go b/src/internal/fuzz/pcg.go
index b8251043f..a6fa42f 100644
--- a/src/internal/fuzz/pcg.go
+++ b/src/internal/fuzz/pcg.go
@@ -44,8 +44,8 @@
func godebugSeed() *int {
debug := strings.Split(os.Getenv("GODEBUG"), ",")
for _, f := range debug {
- if strings.HasPrefix(f, "fuzzseed=") {
- seed, err := strconv.Atoi(strings.TrimPrefix(f, "fuzzseed="))
+ if after, ok := strings.CutPrefix(f, "fuzzseed="); ok {
+ seed, err := strconv.Atoi(after)
if err != nil {
panic("malformed fuzzseed")
}
diff --git a/src/internal/testenv/testenv_test.go b/src/internal/testenv/testenv_test.go
index 769db3a..0a8adf8 100644
--- a/src/internal/testenv/testenv_test.go
+++ b/src/internal/testenv/testenv_test.go
@@ -176,8 +176,8 @@
cmd = testenv.CleanCmdEnv(cmd)

for _, env := range cmd.Env {
- if strings.HasPrefix(env, "PWD=") {
- pwd := strings.TrimPrefix(env, "PWD=")
+ if after, ok := strings.CutPrefix(env, "PWD="); ok {
+ pwd := after
if pwd != dir {
t.Errorf("unexpected PWD: want %s, got %s", dir, pwd)
}
diff --git a/src/os/exec/exec_posix_test.go b/src/os/exec/exec_posix_test.go
index 0711fac..e37014e 100644
--- a/src/os/exec/exec_posix_test.go
+++ b/src/os/exec/exec_posix_test.go
@@ -176,8 +176,8 @@

var pwds []string
for _, kv := range cmd.Environ() {
- if strings.HasPrefix(kv, "PWD=") {
- pwds = append(pwds, strings.TrimPrefix(kv, "PWD="))
+ if after, ok := strings.CutPrefix(kv, "PWD="); ok {
+ pwds = append(pwds, after)
}
}

@@ -253,8 +253,8 @@

var pwds []string
for _, kv := range cmd.Environ() {
- if strings.HasPrefix(kv, "PWD=") {
- pwds = append(pwds, strings.TrimPrefix(kv, "PWD="))
+ if after, ok := strings.CutPrefix(kv, "PWD="); ok {
+ pwds = append(pwds, after)
}
}

diff --git a/src/text/template/exec_test.go b/src/text/template/exec_test.go
index 0eec58a..0e25a67 100644
--- a/src/text/template/exec_test.go
+++ b/src/text/template/exec_test.go
@@ -1751,8 +1751,8 @@
"Nil": nil,
"Zero": 0,
})
- if strings.HasPrefix(tt.out, "ERROR:") {
- e := strings.TrimSpace(strings.TrimPrefix(tt.out, "ERROR:"))
+ if after, ok := strings.CutPrefix(tt.out, "ERROR:"); ok {
+ e := strings.TrimSpace(after)
if err == nil || !strings.Contains(err.Error(), e) {
t.Errorf("%s: Execute: %v, want error %q", tt.text, err, e)
}

Change information

Files:
  • M src/html/template/exec_test.go
  • M src/internal/fuzz/pcg.go
  • M src/internal/testenv/testenv_test.go
  • M src/os/exec/exec_posix_test.go
  • M src/text/template/exec_test.go
Change size: S
Delta: 5 files changed, 12 insertions(+), 12 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: Iba6d0450b035a17ba2bb8a68b0811b9bfe9d4f00
Gerrit-Change-Number: 730963
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

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

Kirill Kolyshkin voted Commit-Queue+1

Commit-Queue+1
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: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Iba6d0450b035a17ba2bb8a68b0811b9bfe9d4f00
Gerrit-Change-Number: 730963
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 01:08:46 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 2:20:06 PM (22 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Go LUCI, Brad Fitzpatrick, Ian Lance Taylor, Roland Shoemaker, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Brad Fitzpatrick, Ian Lance Taylor and Roland Shoemaker

Kirill Kolyshkin voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Brad Fitzpatrick
  • Ian Lance Taylor
  • Roland Shoemaker
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: Iba6d0450b035a17ba2bb8a68b0811b9bfe9d4f00
Gerrit-Change-Number: 730963
Gerrit-PatchSet: 2
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Comment-Date: Thu, 18 Dec 2025 19:20:02 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 8:05:29 PM (16 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Go LUCI, Brad Fitzpatrick, Ian Lance Taylor, Roland Shoemaker, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Brad Fitzpatrick, Ian Lance Taylor and Roland Shoemaker

Kirill Kolyshkin voted and added 1 comment

Votes added by Kirill Kolyshkin

Hold+1

1 comment

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

Hold for Go 1.26 freeze

Open in Gerrit

Related details

Attention is currently required from:
  • Brad Fitzpatrick
  • Ian Lance Taylor
  • Roland Shoemaker
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Holds
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement 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: Iba6d0450b035a17ba2bb8a68b0811b9bfe9d4f00
    Gerrit-Change-Number: 730963
    Gerrit-PatchSet: 2
    Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Reviewer: Roland Shoemaker <rol...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Roland Shoemaker <rol...@golang.org>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Comment-Date: Fri, 19 Dec 2025 01:05:25 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages