[go] all: replace []byte(fmt.Sprintf) with fmt.Appendf

3 views
Skip to first unread message

Kirill Kolyshkin (Gerrit)

unread,
Dec 17, 2025, 8:03:32 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 []byte(fmt.Sprintf) with fmt.Appendf

Replace []byte(fmt.Sprintf(...)) with fmt.Appendf(nil, ...), introduced
in Go 1.19. This avoids the intermediate allocation of a string by
Sprintf, making the code more efficient. Do the similar thing with
fmt.Sprint.

Generated by

go fix -fmtappendf ./...

using

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

Change diff

diff --git a/src/crypto/rsa/rsa_test.go b/src/crypto/rsa/rsa_test.go
index 55aad05..affdf56 100644
--- a/src/crypto/rsa/rsa_test.go
+++ b/src/crypto/rsa/rsa_test.go
@@ -975,7 +975,7 @@
priv.D = d

for j, message := range test.msgs {
- label := []byte(fmt.Sprintf("hi#%d", j))
+ label := fmt.Appendf(nil, "hi#%d", j)
enc, err := EncryptOAEP(sha256, rand.Reader, &priv.PublicKey, message.in, label)
if err != nil {
t.Errorf("#%d,%d: EncryptOAEP: %v", i, j, err)
diff --git a/src/database/sql/sql_test.go b/src/database/sql/sql_test.go
index 2cb2de0..c38774d 100644
--- a/src/database/sql/sql_test.go
+++ b/src/database/sql/sql_test.go
@@ -5013,7 +5013,7 @@
}

func (t issue69728Type) Value() (driver.Value, error) {
- return []byte(fmt.Sprintf("%d, %s", t.ID, t.Name)), nil
+ return fmt.Appendf(nil, "%d, %s", t.ID, t.Name), nil
}

func TestIssue69728(t *testing.T) {
diff --git a/src/encoding/gob/gobencdec_test.go b/src/encoding/gob/gobencdec_test.go
index 1ac7a16..a79ee84 100644
--- a/src/encoding/gob/gobencdec_test.go
+++ b/src/encoding/gob/gobencdec_test.go
@@ -103,7 +103,7 @@
}

func (g *Gobber) GobEncode() ([]byte, error) {
- return []byte(fmt.Sprintf("VALUE=%d", *g)), nil
+ return fmt.Appendf(nil, "VALUE=%d", *g), nil
}

func (g *Gobber) GobDecode(data []byte) error {
@@ -112,7 +112,7 @@
}

func (g *BinaryGobber) MarshalBinary() ([]byte, error) {
- return []byte(fmt.Sprintf("VALUE=%d", *g)), nil
+ return fmt.Appendf(nil, "VALUE=%d", *g), nil
}

func (g *BinaryGobber) UnmarshalBinary(data []byte) error {
@@ -121,7 +121,7 @@
}

func (g *TextGobber) MarshalText() ([]byte, error) {
- return []byte(fmt.Sprintf("VALUE=%d", *g)), nil
+ return fmt.Appendf(nil, "VALUE=%d", *g), nil
}

func (g *TextGobber) UnmarshalText(data []byte) error {
@@ -130,7 +130,7 @@
}

func (v ValueGobber) GobEncode() ([]byte, error) {
- return []byte(fmt.Sprintf("VALUE=%s", v)), nil
+ return fmt.Appendf(nil, "VALUE=%s", v), nil
}

func (v *ValueGobber) GobDecode(data []byte) error {
@@ -139,7 +139,7 @@
}

func (v BinaryValueGobber) MarshalBinary() ([]byte, error) {
- return []byte(fmt.Sprintf("VALUE=%s", v)), nil
+ return fmt.Appendf(nil, "VALUE=%s", v), nil
}

func (v *BinaryValueGobber) UnmarshalBinary(data []byte) error {
@@ -148,7 +148,7 @@
}

func (v TextValueGobber) MarshalText() ([]byte, error) {
- return []byte(fmt.Sprintf("VALUE=%s", v)), nil
+ return fmt.Appendf(nil, "VALUE=%s", v), nil
}

func (v *TextValueGobber) UnmarshalText(data []byte) error {
diff --git a/src/encoding/json/decode_test.go b/src/encoding/json/decode_test.go
index 5c29f6b..67e2004 100644
--- a/src/encoding/json/decode_test.go
+++ b/src/encoding/json/decode_test.go
@@ -145,7 +145,7 @@
type u8marshal uint8

func (u8 u8marshal) MarshalText() ([]byte, error) {
- return []byte(fmt.Sprintf("u%d", u8)), nil
+ return fmt.Appendf(nil, "u%d", u8), nil
}

var errMissingU8Prefix = errors.New("missing 'u' prefix")
@@ -294,7 +294,7 @@
type byteWithMarshalJSON byte

func (b byteWithMarshalJSON) MarshalJSON() ([]byte, error) {
- return []byte(fmt.Sprintf(`"Z%.2x"`, byte(b))), nil
+ return fmt.Appendf(nil, `"Z%.2x"`, byte(b)), nil
}

func (b *byteWithMarshalJSON) UnmarshalJSON(data []byte) error {
@@ -322,7 +322,7 @@
type byteWithMarshalText byte

func (b byteWithMarshalText) MarshalText() ([]byte, error) {
- return []byte(fmt.Sprintf(`Z%.2x`, byte(b))), nil
+ return fmt.Appendf(nil, `Z%.2x`, byte(b)), nil
}

func (b *byteWithMarshalText) UnmarshalText(data []byte) error {
@@ -350,7 +350,7 @@
type intWithMarshalJSON int

func (b intWithMarshalJSON) MarshalJSON() ([]byte, error) {
- return []byte(fmt.Sprintf(`"Z%.2x"`, int(b))), nil
+ return fmt.Appendf(nil, `"Z%.2x"`, int(b)), nil
}

func (b *intWithMarshalJSON) UnmarshalJSON(data []byte) error {
@@ -378,7 +378,7 @@
type intWithMarshalText int

func (b intWithMarshalText) MarshalText() ([]byte, error) {
- return []byte(fmt.Sprintf(`Z%.2x`, int(b))), nil
+ return fmt.Appendf(nil, `Z%.2x`, int(b)), nil
}

func (b *intWithMarshalText) UnmarshalText(data []byte) error {
diff --git a/src/expvar/expvar_test.go b/src/expvar/expvar_test.go
index 24e2029..b944bf8 100644
--- a/src/expvar/expvar_test.go
+++ b/src/expvar/expvar_test.go
@@ -335,7 +335,7 @@
func BenchmarkMapSetDifferentRandom(b *testing.B) {
keys := make([]string, 100)
for i := range keys {
- keys[i] = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprint(i))))
+ keys[i] = fmt.Sprintf("%x", sha1.Sum(fmt.Append(nil, i)))
}

v := new(Int)
@@ -406,7 +406,7 @@
func BenchmarkMapAddDifferentRandom(b *testing.B) {
keys := make([]string, 100)
for i := range keys {
- keys[i] = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprint(i))))
+ keys[i] = fmt.Sprintf("%x", sha1.Sum(fmt.Append(nil, i)))
}

b.ResetTimer()
diff --git a/src/internal/trace/internal/testgen/trace.go b/src/internal/trace/internal/testgen/trace.go
index 2eade48..90a0a20 100644
--- a/src/internal/trace/internal/testgen/trace.go
+++ b/src/internal/trace/internal/testgen/trace.go
@@ -137,7 +137,7 @@
// Expectation file contents.
expect := []byte("SUCCESS\n")
if t.bad {
- expect = []byte(fmt.Sprintf("FAILURE %q\n", t.badMatch))
+ expect = fmt.Appendf(nil, "FAILURE %q\n", t.badMatch)
}

// Create the test file's contents.
diff --git a/src/log/slog/json_handler_test.go b/src/log/slog/json_handler_test.go
index fd6b2e3..a18f4d5 100644
--- a/src/log/slog/json_handler_test.go
+++ b/src/log/slog/json_handler_test.go
@@ -64,7 +64,7 @@
if j.s == "" {
return nil, errors.New("json: empty string")
}
- return []byte(fmt.Sprintf(`[%q]`, j.s)), nil
+ return fmt.Appendf(nil, `[%q]`, j.s), nil
}

type jsonMarshalerError struct {
diff --git a/src/log/slog/text_handler_test.go b/src/log/slog/text_handler_test.go
index e148baa..9ccab20 100644
--- a/src/log/slog/text_handler_test.go
+++ b/src/log/slog/text_handler_test.go
@@ -119,7 +119,7 @@
if t.s == "" {
return nil, errors.New("text: empty string")
}
- return []byte(fmt.Sprintf("text{%q}", t.s)), nil
+ return fmt.Appendf(nil, "text{%q}", t.s), nil
}

func TestTextHandlerPreformatted(t *testing.T) {
diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go
index df691ae..552d587 100644
--- a/src/net/http/serve_test.go
+++ b/src/net/http/serve_test.go
@@ -2112,11 +2112,11 @@
defer afterTest(t)
conn := new(testConn)
body := strings.Repeat("x", 100<<10)
- conn.readBuf.Write([]byte(fmt.Sprintf(
+ conn.readBuf.Write(fmt.Appendf(nil,
"POST / HTTP/1.1\r\n"+
"Host: test\r\n"+
"Content-Length: %d\r\n"+
- "\r\n", len(body))))
+ "\r\n", len(body)))
conn.readBuf.Write([]byte(body))

done := make(chan bool)
@@ -2155,11 +2155,11 @@
}
conn := new(testConn)
body := strings.Repeat("x", 1<<20)
- conn.readBuf.Write([]byte(fmt.Sprintf(
+ conn.readBuf.Write(fmt.Appendf(nil,
"POST / HTTP/1.1\r\n"+
"Host: test\r\n"+
"Content-Length: %d\r\n"+
- "\r\n", len(body))))
+ "\r\n", len(body)))
conn.readBuf.Write([]byte(body))
conn.closec = make(chan bool, 1)

@@ -2306,12 +2306,12 @@
cw.Close()
conn.readBuf.WriteString("\r\n")
} else {
- conn.readBuf.Write([]byte(fmt.Sprintf(
+ conn.readBuf.Write(fmt.Appendf(nil,
"POST / HTTP/1.1\r\n"+
"Host: test\r\n"+
tt.connectionHeader()+
"Content-Length: %d\r\n"+
- "\r\n", len(body))))
+ "\r\n", len(body)))
conn.readBuf.Write([]byte(body))
}
if !tt.reqConnClose {
@@ -3423,7 +3423,7 @@
t.Logf("set RST avoidance delay to %v", timeout)

const bodySize = 5 << 20
- req := []byte(fmt.Sprintf("POST / HTTP/1.1\r\nHost: foo.com\r\nContent-Length: %d\r\n\r\n", bodySize))
+ req := fmt.Appendf(nil, "POST / HTTP/1.1\r\nHost: foo.com\r\nContent-Length: %d\r\n\r\n", bodySize)
for range bodySize {
req = append(req, 'x')
}

Change information

Files:
  • M src/crypto/rsa/rsa_test.go
  • M src/database/sql/sql_test.go
  • M src/encoding/gob/gobencdec_test.go
  • M src/encoding/json/decode_test.go
  • M src/expvar/expvar_test.go
  • M src/internal/trace/internal/testgen/trace.go
  • M src/log/slog/json_handler_test.go
  • M src/log/slog/text_handler_test.go
  • M src/net/http/serve_test.go
Change size: M
Delta: 9 files changed, 25 insertions(+), 25 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: I4c438882306156ef9d200a68e4596bc6ebfe7f4b
Gerrit-Change-Number: 730966
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Griesemer (Gerrit)

unread,
Dec 18, 2025, 12:46:53 PM (24 hours ago) Dec 18
to Kirill Kolyshkin, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Jonathan Amsterdam, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Brad Fitzpatrick, Jonathan Amsterdam, Kirill Kolyshkin and Russ Cox

Robert Griesemer voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Brad Fitzpatrick
  • Jonathan Amsterdam
  • Kirill Kolyshkin
  • Russ Cox
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: I4c438882306156ef9d200a68e4596bc6ebfe7f4b
Gerrit-Change-Number: 730966
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@google.com>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Russ Cox <r...@golang.org>
Gerrit-Attention: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Jonathan Amsterdam <j...@google.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 17:46:49 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Griesemer (Gerrit)

unread,
Dec 18, 2025, 12:49:01 PM (24 hours ago) Dec 18
to Kirill Kolyshkin, goph...@pubsubhelper.golang.org, Russ Cox, Go LUCI, Brad Fitzpatrick, Jonathan Amsterdam, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Brad Fitzpatrick, Jonathan Amsterdam and Kirill Kolyshkin

Robert Griesemer removed Russ Cox from this change

Deleted Reviewers:
  • Russ Cox
Open in Gerrit

Related details

Attention is currently required from:
  • Brad Fitzpatrick
  • Jonathan Amsterdam
  • 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: deleteReviewer
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I4c438882306156ef9d200a68e4596bc6ebfe7f4b
Gerrit-Change-Number: 730966
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 3:44:14 PM (21 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Go LUCI, Robert Griesemer, Brad Fitzpatrick, Jonathan Amsterdam, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Brad Fitzpatrick, Jonathan Amsterdam and Robert Griesemer

Kirill Kolyshkin voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Brad Fitzpatrick
  • Jonathan Amsterdam
  • 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: I4c438882306156ef9d200a68e4596bc6ebfe7f4b
Gerrit-Change-Number: 730966
Gerrit-PatchSet: 2
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
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: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Jonathan Amsterdam <j...@google.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 20:44:11 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 4:11:13 PM (20 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Go LUCI, Robert Griesemer, Brad Fitzpatrick, Jonathan Amsterdam, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Brad Fitzpatrick, Jonathan Amsterdam and Robert Griesemer

Kirill Kolyshkin voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Brad Fitzpatrick
  • Jonathan Amsterdam
  • 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: I4c438882306156ef9d200a68e4596bc6ebfe7f4b
Gerrit-Change-Number: 730966
Gerrit-PatchSet: 3
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
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: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Jonathan Amsterdam <j...@google.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 21:11:10 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 8:06:17 PM (16 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Go LUCI, Robert Griesemer, Brad Fitzpatrick, Jonathan Amsterdam, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Brad Fitzpatrick, Jonathan Amsterdam 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:
  • Brad Fitzpatrick
  • Jonathan Amsterdam
  • Robert Griesemer
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: I4c438882306156ef9d200a68e4596bc6ebfe7f4b
    Gerrit-Change-Number: 730966
    Gerrit-PatchSet: 3
    Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
    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: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Attention: Jonathan Amsterdam <j...@google.com>
    Gerrit-Comment-Date: Fri, 19 Dec 2025 01:06:13 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages