[go] html/template,text/template: use errors.New instead of fmt.Errorf

1 view
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Oct 1, 2025, 4:51:28 PM (12 hours ago) Oct 1
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

html/template,text/template: use errors.New instead of fmt.Errorf

For non-formatted strings "fmt.Errorf" and "errors.New" have the same result,
but the former is slower and allocates more memory.

So it is better to call "errors.New" directly for non-formatted strings.
Change-Id: I859511840886cf4f5af5e3b7d9090b1163db403c
GitHub-Last-Rev: a0d7e6984b244d66018424567d53d3f04cddc4de
GitHub-Pull-Request: golang/go#75719

Change diff

diff --git a/src/html/template/template.go b/src/html/template/template.go
index 2440fec..e370e9f 100644
--- a/src/html/template/template.go
+++ b/src/html/template/template.go
@@ -5,6 +5,7 @@
package template

import (
+ "errors"
"fmt"
"io"
"io/fs"
@@ -31,7 +32,7 @@
}

// escapeOK is a sentinel value used to indicate valid escaping.
-var escapeOK = fmt.Errorf("template escaped correctly")
+var escapeOK = errors.New("template escaped correctly")

// nameSpace is the data structure shared by all templates in an association.
type nameSpace struct {
@@ -87,7 +88,7 @@
t.nameSpace.mu.Lock()
defer t.nameSpace.mu.Unlock()
if t.nameSpace.escaped {
- return fmt.Errorf("html/template: cannot Parse after Execute")
+ return errors.New("html/template: cannot Parse after Execute")
}
return nil
}
@@ -404,7 +405,7 @@

if len(filenames) == 0 {
// Not really a problem, but be consistent.
- return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
+ return nil, errors.New("html/template: no files named in call to ParseFiles")
}
for _, filename := range filenames {
name, b, err := readFile(filename)
diff --git a/src/text/template/funcs.go b/src/text/template/funcs.go
index c28c3ea..35e0401 100644
--- a/src/text/template/funcs.go
+++ b/src/text/template/funcs.go
@@ -180,7 +180,7 @@
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
x = int64(index.Uint())
case reflect.Invalid:
- return 0, fmt.Errorf("cannot index slice/array with nil")
+ return 0, errors.New("cannot index slice/array with nil")
default:
return 0, fmt.Errorf("cannot index slice/array with type %s", index.Type())
}
@@ -198,13 +198,13 @@
func index(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error) {
item = indirectInterface(item)
if !item.IsValid() {
- return reflect.Value{}, fmt.Errorf("index of untyped nil")
+ return reflect.Value{}, errors.New("index of untyped nil")
}
for _, index := range indexes {
index = indirectInterface(index)
var isNil bool
if item, isNil = indirect(item); isNil {
- return reflect.Value{}, fmt.Errorf("index of nil pointer")
+ return reflect.Value{}, errors.New("index of nil pointer")
}
switch item.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
@@ -242,7 +242,7 @@
func slice(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error) {
item = indirectInterface(item)
if !item.IsValid() {
- return reflect.Value{}, fmt.Errorf("slice of untyped nil")
+ return reflect.Value{}, errors.New("slice of untyped nil")
}
if len(indexes) > 3 {
return reflect.Value{}, fmt.Errorf("too many slice indexes: %d", len(indexes))
@@ -251,7 +251,7 @@
switch item.Kind() {
case reflect.String:
if len(indexes) == 3 {
- return reflect.Value{}, fmt.Errorf("cannot 3-index slice a string")
+ return reflect.Value{}, errors.New("cannot 3-index slice a string")
}
cap = item.Len()
case reflect.Array, reflect.Slice:
@@ -288,7 +288,7 @@
func length(item reflect.Value) (int, error) {
item, isNil := indirect(item)
if isNil {
- return 0, fmt.Errorf("len of nil pointer")
+ return 0, errors.New("len of nil pointer")
}
switch item.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
@@ -308,7 +308,7 @@
func call(name string, fn reflect.Value, args ...reflect.Value) (reflect.Value, error) {
fn = indirectInterface(fn)
if !fn.IsValid() {
- return reflect.Value{}, fmt.Errorf("call of nil")
+ return reflect.Value{}, errors.New("call of nil")
}
typ := fn.Type()
if typ.Kind() != reflect.Func {
diff --git a/src/text/template/helper.go b/src/text/template/helper.go
index 81b5553..8985840 100644
--- a/src/text/template/helper.go
+++ b/src/text/template/helper.go
@@ -7,6 +7,7 @@
package template

import (
+ "errors"
"fmt"
"io/fs"
"os"
@@ -62,7 +63,7 @@
func parseFiles(t *Template, readFile func(string) (string, []byte, error), filenames ...string) (*Template, error) {
if len(filenames) == 0 {
// Not really a problem, but be consistent.
- return nil, fmt.Errorf("template: no files named in call to ParseFiles")
+ return nil, errors.New("template: no files named in call to ParseFiles")
}
for _, filename := range filenames {
name, b, err := readFile(filename)

Change information

Files:
  • M src/html/template/template.go
  • M src/text/template/funcs.go
  • M src/text/template/helper.go
Change size: S
Delta: 3 files changed, 13 insertions(+), 11 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: I859511840886cf4f5af5e3b7d9090b1163db403c
Gerrit-Change-Number: 708496
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Oct 1, 2025, 4:51:30 PM (12 hours ago) Oct 1
to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems with your PR:

  1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?

Please address any problems by updating the GitHub PR.

When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

For more details, see:

(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not 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: I859511840886cf4f5af5e3b7d9090b1163db403c
    Gerrit-Change-Number: 708496
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Wed, 01 Oct 2025 20:51:25 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    kyose chang (Gerrit)

    unread,
    Oct 1, 2025, 4:56:41 PM (11 hours ago) Oct 1
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com

    kyose chang added 1 comment

    Patchset-level comments
    Gopher Robot . resolved

    I spotted some possible problems with your PR:

      1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?

    Please address any problems by updating the GitHub PR.

    When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

    To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

    For more details, see:

    (In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

    kyose chang

    Acknowledged

    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: I859511840886cf4f5af5e3b7d9090b1163db403c
      Gerrit-Change-Number: 708496
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-CC: kyose chang <sev...@gmail.com>
      Gerrit-Comment-Date: Wed, 01 Oct 2025 20:56:31 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Gopher Robot <go...@golang.org>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages