From: Bjørn Erik Pedersen Date: Sat, 7 Mar 2026 15:37:47 +0000 (+0100) Subject: all: Run go fix ./... X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=e3108225bf2f49bd3908614ac120bf1699fde241;p=brevno-suite%2Fhugo all: Run go fix ./... --- diff --git a/codegen/methods.go b/codegen/methods.go index 08ac97b00..d705914bc 100644 --- a/codegen/methods.go +++ b/codegen/methods.go @@ -73,7 +73,7 @@ func (c *Inspector) MethodsFromTypes(include []reflect.Type, exclude []reflect.T nameAndPackage := func(t reflect.Type) (string, string) { var name, pkg string - isPointer := t.Kind() == reflect.Ptr + isPointer := t.Kind() == reflect.Pointer if isPointer { t = t.Elem() diff --git a/codegen/methods_test.go b/codegen/methods_test.go index 0aff43d0e..bce80ec03 100644 --- a/codegen/methods_test.go +++ b/codegen/methods_test.go @@ -26,9 +26,9 @@ import ( func TestMethods(t *testing.T) { var ( - zeroIE = reflect.TypeOf((*IEmbed)(nil)).Elem() - zeroIEOnly = reflect.TypeOf((*IEOnly)(nil)).Elem() - zeroI = reflect.TypeOf((*I)(nil)).Elem() + zeroIE = reflect.TypeFor[IEmbed]() + zeroIEOnly = reflect.TypeFor[IEOnly]() + zeroI = reflect.TypeFor[I]() ) dir, _ := os.Getwd() diff --git a/common/hashing/hashing_test.go b/common/hashing/hashing_test.go index 8a165786a..de2fed8f0 100644 --- a/common/hashing/hashing_test.go +++ b/common/hashing/hashing_test.go @@ -38,9 +38,7 @@ func TestXxHashFromReaderPara(t *testing.T) { var wg sync.WaitGroup for i := range 10 { - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { for j := range 100 { s := strings.Repeat("Hello ", i+j+1*42) r := strings.NewReader(s) @@ -50,7 +48,7 @@ func TestXxHashFromReaderPara(t *testing.T) { expect, _ := XXHashFromString(s) c.Assert(got, qt.Equals, expect) } - }() + }) } wg.Wait() diff --git a/common/herrors/file_error_test.go b/common/herrors/file_error_test.go index 7aca08405..e7e66ca8f 100644 --- a/common/herrors/file_error_test.go +++ b/common/herrors/file_error_test.go @@ -32,15 +32,15 @@ func TestNewFileError(t *testing.T) { fe := NewFileErrorFromName(errors.New("bar"), "foo.html") c.Assert(fe.Error(), qt.Equals, `"foo.html:1:1": bar`) - lines := "" + var lines strings.Builder for i := 1; i <= 100; i++ { - lines += fmt.Sprintf("line %d\n", i) + lines.WriteString(fmt.Sprintf("line %d\n", i)) } fe.UpdatePosition(text.Position{LineNumber: 32, ColumnNumber: 2}) c.Assert(fe.Error(), qt.Equals, `"foo.html:32:2": bar`) fe.UpdatePosition(text.Position{LineNumber: 0, ColumnNumber: 0, Offset: 212}) - fe.UpdateContent(strings.NewReader(lines), nil) + fe.UpdateContent(strings.NewReader(lines.String()), nil) c.Assert(fe.Error(), qt.Equals, `"foo.html:32:0": bar`) errorContext := fe.ErrorContext() c.Assert(errorContext, qt.IsNotNil) diff --git a/common/hreflect/convert.go b/common/hreflect/convert.go index cb5233482..e317d4aed 100644 --- a/common/hreflect/convert.go +++ b/common/hreflect/convert.go @@ -96,7 +96,7 @@ func errConvert(v reflect.Value, s string) error { // See Issue 14079. func ConvertIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool) { switch val.Kind() { - case reflect.Ptr, reflect.Interface: + case reflect.Pointer, reflect.Interface: if val.IsNil() { // Return typ's zero value. return reflect.Zero(typ), true diff --git a/common/hreflect/helpers.go b/common/hreflect/helpers.go index 0a9200007..fe11d2b9f 100644 --- a/common/hreflect/helpers.go +++ b/common/hreflect/helpers.go @@ -27,7 +27,7 @@ import ( // IsInterfaceOrPointer returns whether the given kind is an interface or a pointer. func IsInterfaceOrPointer(kind reflect.Kind) bool { - return kind == reflect.Interface || kind == reflect.Ptr + return kind == reflect.Interface || kind == reflect.Pointer } // TODO(bep) replace the private versions in /tpl with these. @@ -92,7 +92,7 @@ func IsSlice(v any) bool { return reflect.ValueOf(v).Kind() == reflect.Slice } -var zeroType = reflect.TypeOf((*types.Zeroer)(nil)).Elem() +var zeroType = reflect.TypeFor[types.Zeroer]() var isZeroCache sync.Map @@ -136,7 +136,7 @@ func IsTruthfulValue(val reflect.Value) (truth bool) { truth = val.Bool() case reflect.Complex64, reflect.Complex128: truth = val.Complex() != 0 - case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Interface: + case reflect.Chan, reflect.Func, reflect.Pointer, reflect.Interface: truth = !val.IsNil() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: truth = val.Int() != 0 @@ -216,8 +216,8 @@ func GetMethodIndexByName(tp reflect.Type, name string) int { } var ( - timeType = reflect.TypeOf((*time.Time)(nil)).Elem() - asTimeProviderType = reflect.TypeOf((*htime.AsTimeProvider)(nil)).Elem() + timeType = reflect.TypeFor[time.Time]() + asTimeProviderType = reflect.TypeFor[htime.AsTimeProvider]() ) // IsTime returns whether tp is a time.Time type or if it can be converted into one @@ -240,7 +240,7 @@ func IsValid(v reflect.Value) bool { } switch v.Kind() { - case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice: return !v.IsNil() } @@ -347,13 +347,13 @@ func IsNil(v reflect.Value) bool { return true } switch v.Kind() { - case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice: return v.IsNil() } return false } -var contextInterface = reflect.TypeOf((*context.Context)(nil)).Elem() +var contextInterface = reflect.TypeFor[context.Context]() var isContextCache = hmaps.NewCache[reflect.Type, bool]() diff --git a/common/types/evictingqueue_test.go b/common/types/evictingqueue_test.go index b93243f3c..a2fcc8e6f 100644 --- a/common/types/evictingqueue_test.go +++ b/common/types/evictingqueue_test.go @@ -56,9 +56,7 @@ func TestEvictingStringQueueConcurrent(t *testing.T) { queue := NewEvictingQueue[string](3) for range 100 { - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { queue.Add(val) v := queue.Peek() if v != val { @@ -68,7 +66,7 @@ func TestEvictingStringQueueConcurrent(t *testing.T) { if len(vals) != 1 || vals[0] != val { t.Error("wrong val") } - }() + }) } wg.Wait() } diff --git a/common/types/types.go b/common/types/types.go index 3c9c3b60d..733a10946 100644 --- a/common/types/types.go +++ b/common/types/types.go @@ -89,7 +89,7 @@ func IsNil(v any) bool { value := reflect.ValueOf(v) switch value.Kind() { - case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice: return value.IsNil() } diff --git a/config/security/securityConfig.go b/config/security/securityConfig.go index 333cf4cc2..d47492c0e 100644 --- a/config/security/securityConfig.go +++ b/config/security/securityConfig.go @@ -202,7 +202,7 @@ func stringSliceToWhitelistHook() mapstructure.DecodeHookFuncType { t reflect.Type, data any, ) (any, error) { - if t != reflect.TypeOf(Whitelist{}) { + if t != reflect.TypeFor[Whitelist]() { return data, nil } diff --git a/helpers/path.go b/helpers/path.go index 7e7ec0252..87577f4b3 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -109,13 +109,13 @@ func GetDottedRelativePath(inPath string) string { return "./" } - var dottedPath string + var dottedPath strings.Builder for i := 1; i < sectionCount; i++ { - dottedPath += "../" + dottedPath.WriteString("../") } - return dottedPath + return dottedPath.String() } type NamedSlice struct { diff --git a/helpers/path_test.go b/helpers/path_test.go index a1928b2b5..697f420af 100644 --- a/helpers/path_test.go +++ b/helpers/path_test.go @@ -364,7 +364,7 @@ func TestExtractAndGroupRootPaths(t *testing.T) { t.Run("Limits number of root groups", func(t *testing.T) { in := []string{} // Create 15 different root paths to exceed maxRootGroups (10) - for i := 0; i < 15; i++ { + for i := range 15 { in = append(in, filepath.FromSlash(fmt.Sprintf("/path%d/subdir", i))) } @@ -377,8 +377,8 @@ func TestExtractAndGroupRootPaths(t *testing.T) { func BenchmarkExtractAndGroupRootPaths(b *testing.B) { in := []string{} - for i := 0; i < 10; i++ { - for j := 0; j < 1000; j++ { + for i := range 10 { + for j := range 1000 { in = append(in, fmt.Sprintf("/a/b/c/s%d/p%d", i, j)) } } diff --git a/htesting/hqt/checkers.go b/htesting/hqt/checkers.go index f58382fb5..a118967c1 100644 --- a/htesting/hqt/checkers.go +++ b/htesting/hqt/checkers.go @@ -182,7 +182,7 @@ func structTypes(v reflect.Value, m map[reflect.Type]struct{}) { return } switch v.Kind() { - case reflect.Ptr: + case reflect.Pointer: if !v.IsNil() { structTypes(v.Elem(), m) } diff --git a/hugolib/cascade_test.go b/hugolib/cascade_test.go index 8eaa369b6..21d8e20c4 100644 --- a/hugolib/cascade_test.go +++ b/hugolib/cascade_test.go @@ -15,6 +15,7 @@ package hugolib import ( "fmt" + "strings" "testing" "github.com/gohugoio/hugo/hugolib/sitesmatrix" @@ -23,27 +24,28 @@ import ( ) func BenchmarkCascadeTarget(b *testing.B) { - files := ` + var files strings.Builder + files.WriteString(` -- content/_index.md -- background = 'yosemite.jpg' [cascade._target] kind = '{section,term}' -- content/posts/_index.md -- -- content/posts/funny/_index.md -- -` +`) for i := 1; i < 100; i++ { - files += fmt.Sprintf("\n-- content/posts/p%d.md --\n", i+1) + files.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n", i+1)) } for i := 1; i < 100; i++ { - files += fmt.Sprintf("\n-- content/posts/funny/pf%d.md --\n", i+1) + files.WriteString(fmt.Sprintf("\n-- content/posts/funny/pf%d.md --\n", i+1)) } b.Run("Kind", func(b *testing.B) { cfg := IntegrationTestConfig{ T: b, - TxtarString: files, + TxtarString: files.String(), } b.ResetTimer() diff --git a/hugolib/doctree/simpletree_test.go b/hugolib/doctree/simpletree_test.go index fe4c0fa84..e99d348f0 100644 --- a/hugolib/doctree/simpletree_test.go +++ b/hugolib/doctree/simpletree_test.go @@ -21,7 +21,7 @@ import ( func BenchmarkSimpleThreadSafeTree(b *testing.B) { newTestTree := func() TreeThreadSafe[int] { t := NewSimpleThreadSafeTree[int]() - for i := 0; i < 1000; i++ { + for i := range 1000 { t.Insert(fmt.Sprintf("key%d", i), i) } return t diff --git a/hugolib/hugo_sites_build_errors_test.go b/hugolib/hugo_sites_build_errors_test.go index eecd86f8d..3209f664d 100644 --- a/hugolib/hugo_sites_build_errors_test.go +++ b/hugolib/hugo_sites_build_errors_test.go @@ -275,7 +275,6 @@ foo bar } for _, test := range tests { - test := test if test.name != "Base template parse failed" { continue } diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go index d4456aebb..8ea992aa0 100644 --- a/hugolib/hugo_sites_build_test.go +++ b/hugolib/hugo_sites_build_test.go @@ -154,7 +154,7 @@ RegularPagesRecursive: {{ range .RegularPagesRecursive }}{{ .RelPermalink }}|{{ return } - for i := 0; i < sectionsPerLevel; i++ { + for i := range sectionsPerLevel { sectionCount++ sectionName := fmt.Sprintf("s%d", i) sectionPath := sectionName @@ -164,7 +164,7 @@ RegularPagesRecursive: {{ range .RegularPagesRecursive }}{{ .RelPermalink }}|{{ sb.WriteString(section(currentSection, i)) // Pages in this section - for j := 0; j < pagesPerSection; j++ { + for j := range pagesPerSection { pageCount++ sb.WriteString(page(sectionPath, j)) } diff --git a/hugolib/hugo_smoke_test.go b/hugolib/hugo_smoke_test.go index b32af4f44..fb46cff17 100644 --- a/hugolib/hugo_smoke_test.go +++ b/hugolib/hugo_smoke_test.go @@ -781,7 +781,8 @@ func BenchmarkBaseline(b *testing.B) { func benchmarkBaselineFiles(leafBundles bool) string { rnd := rand.New(rand.NewSource(32)) - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "https://example.com" defaultContentLanguage = 'en' @@ -828,7 +829,7 @@ weight = 4 {{ .Content }} -- layouts/_shortcodes/myshort.html -- {{ .Inner }} -` +`) contentTemplate := ` --- @@ -855,11 +856,11 @@ Aliqua labore enim et sint anim amet excepteur ea dolore. ` for _, lang := range []string{"en", "nn", "no", "sv"} { - files += fmt.Sprintf("\n-- content/%s/_index.md --\n"+contentTemplate, lang, 1, 1, 1) + files.WriteString(fmt.Sprintf("\n-- content/%s/_index.md --\n"+contentTemplate, lang, 1, 1, 1)) for i, root := range []string{"", "foo", "bar", "baz"} { for j, section := range []string{"posts", "posts/funny", "posts/science", "posts/politics", "posts/world", "posts/technology", "posts/world/news", "posts/world/news/europe"} { n := i + j + 1 - files += fmt.Sprintf("\n-- content/%s/%s/%s/_index.md --\n"+contentTemplate, lang, root, section, n, n, n) + files.WriteString(fmt.Sprintf("\n-- content/%s/%s/%s/_index.md --\n"+contentTemplate, lang, root, section, n, n, n)) for k := 1; k < rnd.Intn(30)+1; k++ { n := n + k ns := fmt.Sprintf("%d", n) @@ -867,11 +868,11 @@ Aliqua labore enim et sint anim amet excepteur ea dolore. ns = fmt.Sprintf("%d/index", n) } file := fmt.Sprintf("\n-- content/%s/%s/%s/p%s.md --\n"+contentTemplate, lang, root, section, ns, n, n) - files += file + files.WriteString(file) } } } } - return files + return files.String() } diff --git a/hugolib/language_test.go b/hugolib/language_test.go index eb7f218f8..e242fc907 100644 --- a/hugolib/language_test.go +++ b/hugolib/language_test.go @@ -38,7 +38,6 @@ func TestI18n(t *testing.T) { } for _, tc := range testCases { - tc := tc c.Run(tc.name, func(c *qt.C) { files := fmt.Sprintf(` -- hugo.toml -- diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 6783bc128..0d9c99505 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -320,15 +320,15 @@ func normalizeExpected(ext, str string) string { return strings.Trim(tpl.StripHTML(str), " ") case "ad": paragraphs := strings.Split(str, "

") - expected := "" + var expected strings.Builder for _, para := range paragraphs { if para == "" { continue } - expected += fmt.Sprintf("
\n%s

\n", para) + expected.WriteString(fmt.Sprintf("
\n%s

\n", para)) } - return expected + return expected.String() case "rst": if str == "" { return "
" @@ -364,23 +364,24 @@ func testAllMarkdownEnginesForPages(t *testing.T, panic("contentDir must be set to 'content' for this test") } - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- [security] [security.exec] allow = ['^python$', '^rst2html.*', '^asciidoctor$'] -` +`) for i, source := range pageSources { - files += fmt.Sprintf("-- content/p%d.%s --\n%s\n", i, e.ext, source) + files.WriteString(fmt.Sprintf("-- content/p%d.%s --\n%s\n", i, e.ext, source)) } homePath := fmt.Sprintf("_index.%s", e.ext) - files += fmt.Sprintf("-- content/%s --\n%s\n", homePath, homePage) + files.WriteString(fmt.Sprintf("-- content/%s --\n%s\n", homePath, homePage)) b := NewIntegrationTestBuilder( IntegrationTestConfig{ T: t, - TxtarString: files, + TxtarString: files.String(), NeedsOsFS: true, BaseCfg: cfg, }, @@ -513,7 +514,8 @@ baseURL = "http://example.com/" func TestPageDatesSections(t *testing.T) { t.Parallel() - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "http://example.com/" -- content/no-index/page.md -- @@ -540,18 +542,18 @@ date: 2018-01-15 title: Date date: 2018-01-15 --- -` +`) for i := 1; i <= 20; i++ { - files += fmt.Sprintf(` + files.WriteString(fmt.Sprintf(` -- content/main-section/p%d.md -- --- title: Date date: 2012-01-12 --- -`, i) +`, i)) } - b := Test(t, files) + b := Test(t, files.String()) b.Assert(len(b.H.Sites), qt.Equals, 1) s := b.H.Sites[0] @@ -1261,7 +1263,6 @@ post = ":year/:month/:day/:title/" } for i, test := range tests { - test := test t.Run(fmt.Sprintf("Test%d", i), func(t *testing.T) { t.Parallel() diff --git a/hugolib/pages_test.go b/hugolib/pages_test.go index bfa67ad9b..35b82b386 100644 --- a/hugolib/pages_test.go +++ b/hugolib/pages_test.go @@ -3,6 +3,7 @@ package hugolib import ( "fmt" "math/rand" + "strings" "testing" "github.com/gohugoio/hugo/resources/page" @@ -22,17 +23,18 @@ categories: %s --- ` - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "http://example.com/" -` +`) for i := 1; i <= numPages; i++ { - files += fmt.Sprintf("-- content/page%d.md --%s\n", i, fmt.Sprintf(pageTemplate, i, rand.Intn(numPages), categoriesSlice)) + files.WriteString(fmt.Sprintf("-- content/page%d.md --%s\n", i, fmt.Sprintf(pageTemplate, i, rand.Intn(numPages), categoriesSlice))) } - return Test(t, files, TestOptSkipRender()) + return Test(t, files.String(), TestOptSkipRender()) } func TestPagesPrevNext(t *testing.T) { diff --git a/hugolib/paginator_test.go b/hugolib/paginator_test.go index 7c860c269..f049dddd4 100644 --- a/hugolib/paginator_test.go +++ b/hugolib/paginator_test.go @@ -15,13 +15,15 @@ package hugolib import ( "fmt" + "strings" "testing" qt "github.com/frankban/quicktest" ) func TestPaginator(t *testing.T) { - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "https://example.com/foo/" @@ -36,28 +38,28 @@ contentDir = "content/en" [languages.nn] weight = 2 contentDir = "content/nn" -` +`) // Manually generate content files for the txtar string - for i := 0; i < 9; i++ { - files += fmt.Sprintf(` + for i := range 9 { + files.WriteString(fmt.Sprintf(` -- content/en/blog/page%d.md -- --- title: Page %d --- Content. -`, i, i) - files += fmt.Sprintf(` +`, i, i)) + files.WriteString(fmt.Sprintf(` -- content/nn/blog/page%d.md -- --- title: Page %d --- Content. -`, i, i) +`, i, i)) } - files += ` + files.WriteString(` -- layouts/home.html -- {{ $pag := $.Paginator }} Total: {{ $pag.TotalPages }} @@ -80,9 +82,9 @@ URL: {{ $pag.URL }} {{ range $i, $e := $pag.Pagers }} {{ printf "%d: %d/%d %t" $i $pag.PageNumber .PageNumber (eq . $pag) -}} {{ end }} -` +`) - b := Test(t, files) + b := Test(t, files.String()) b.AssertFileContent("public/index.html", "Page Number: 1", @@ -131,7 +133,8 @@ Paginate: {{ range (.Paginate (sort .Site.RegularPages ".File.Filename" "desc")) // https://github.com/gohugoio/hugo/issues/6797 func TestPaginateOutputFormat(t *testing.T) { - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "http://example.com/" -- content/_index.md -- @@ -141,23 +144,23 @@ cascade: outputs: - JSON --- -` - for i := 0; i < 22; i++ { - files += fmt.Sprintf(` +`) + for i := range 22 { + files.WriteString(fmt.Sprintf(` -- content/p%d.md -- --- title: "Page" weight: %d --- -`, i+1, i+1) +`, i+1, i+1)) } - files += ` + files.WriteString(` -- layouts/index.json -- JSON: {{ .Paginator.TotalNumberOfElements }}: {{ range .Paginator.Pages }}|{{ .RelPermalink }}{{ end }}:DONE -` +`) - b := Test(t, files) + b := Test(t, files.String()) b.AssertFileContent("public/index.json", `JSON: 22 diff --git a/hugolib/rebuild_test.go b/hugolib/rebuild_test.go index bf2c13a3c..d376c162c 100644 --- a/hugolib/rebuild_test.go +++ b/hugolib/rebuild_test.go @@ -1778,7 +1778,8 @@ Home. } func benchmarkFilesEdit(count int) string { - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "https://example.com" disableKinds = ["term", "taxonomy"] @@ -1791,7 +1792,7 @@ List: {{ .Title }}|{{ .Content }}| --- title: "My Sect" --- - ` + `) contentTemplate := ` --- @@ -1801,10 +1802,10 @@ P%d Content. ` for i := range count { - files += fmt.Sprintf("-- content/mysect/p%d/index.md --\n%s", i, fmt.Sprintf(contentTemplate, i, i)) + files.WriteString(fmt.Sprintf("-- content/mysect/p%d/index.md --\n%s", i, fmt.Sprintf(contentTemplate, i, i))) } - return files + return files.String() } func BenchmarkRebuildContentFileChange(b *testing.B) { diff --git a/hugolib/resource_chain_test.go b/hugolib/resource_chain_test.go index 978fa3713..ba6f30d76 100644 --- a/hugolib/resource_chain_test.go +++ b/hugolib/resource_chain_test.go @@ -1321,7 +1321,6 @@ Template test. } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { if !test.shouldRun() { t.Skip() diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index 78edd8135..c680620cf 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -446,11 +446,11 @@ func doRenderShortcode( } if len(sc.inner) > 0 { - var inner string + var inner strings.Builder for _, innerData := range sc.inner { switch innerData := innerData.(type) { case string: - inner += innerData + inner.WriteString(innerData) case *shortcode: s, err := prepareShortcode(level+1, innerData, data, po, isRenderString) if err != nil { @@ -461,7 +461,7 @@ func doRenderShortcode( if err != nil { return zeroShortcode, err } - inner += ss + inner.WriteString(ss) default: po.p.s.Log.Errorf("Illegal state on shortcode rendering of %q in page %q. Illegal type in inner data: %s ", sc.name, p.File().Path(), reflect.TypeOf(innerData)) @@ -473,7 +473,7 @@ func doRenderShortcode( // shortcode. if sc.doMarkup && (level > 0 || sc.configVersion() == 1) { var err error - b, err := p.pageOutput.contentRenderer.ParseAndRenderContent(ctx, []byte(inner), false) + b, err := p.pageOutput.contentRenderer.ParseAndRenderContent(ctx, []byte(inner.String()), false) if err != nil { return zeroShortcode, err } @@ -492,7 +492,7 @@ func doRenderShortcode( // the newline. switch p.m.pageConfigSource.Content.Markup { case "", "markdown": - if match, _ := regexp.MatchString(innerNewlineRegexp, inner); !match { + if match, _ := regexp.MatchString(innerNewlineRegexp, inner.String()); !match { cleaner, err := regexp.Compile(innerCleanupRegexp) if err == nil { @@ -504,7 +504,7 @@ func doRenderShortcode( // TODO(bep) we may have plain text inner templates. data.Inner = template.HTML(newInner) } else { - data.Inner = template.HTML(inner) + data.Inner = template.HTML(inner.String()) } } diff --git a/hugolib/site_stats_test.go b/hugolib/site_stats_test.go index 79dbfa59a..8f5ce6867 100644 --- a/hugolib/site_stats_test.go +++ b/hugolib/site_stats_test.go @@ -16,6 +16,7 @@ package hugolib import ( "bytes" "fmt" + "strings" "testing" "github.com/gohugoio/hugo/helpers" @@ -28,7 +29,8 @@ func TestSiteStats(t *testing.T) { c := qt.New(t) - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "http://example.com/blog" @@ -56,7 +58,7 @@ List|{{ .Title }}|Pages: {{ .Paginator.TotalPages }}|{{ .Content }} -- layouts/terms.html -- Terms List|{{ .Title }}|{{ .Content }} -` +`) pageTemplate := `--- title: "T%d" @@ -72,16 +74,16 @@ aliases: [/Ali%d] for i := range 2 { for j := range 2 { pageID := i + j + 1 - files += fmt.Sprintf("\n-- content/p%d.md --\n", pageID) - files += fmt.Sprintf(pageTemplate, pageID, fmt.Sprintf("- tag%d", j), fmt.Sprintf("- category%d", j), pageID) + files.WriteString(fmt.Sprintf("\n-- content/p%d.md --\n", pageID)) + files.WriteString(fmt.Sprintf(pageTemplate, pageID, fmt.Sprintf("- tag%d", j), fmt.Sprintf("- category%d", j), pageID)) } } for i := range 5 { - files += fmt.Sprintf("\n-- assets/myimage%d.png --\niVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", i+1) + files.WriteString(fmt.Sprintf("\n-- assets/myimage%d.png --\niVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", i+1)) } - b := Test(t, files) + b := Test(t, files.String()) h := b.H stats := []*helpers.ProcessingStats{ diff --git a/hugolib/sitesmatrix/sitematrix_integration_test.go b/hugolib/sitesmatrix/sitematrix_integration_test.go index 4e7bab846..cf20088d5 100644 --- a/hugolib/sitesmatrix/sitematrix_integration_test.go +++ b/hugolib/sitesmatrix/sitematrix_integration_test.go @@ -1053,7 +1053,8 @@ All. {{ .Title }}|Lang: {{ .Site.Language.Name }}|Ver: {{ .Site.Version.Name }}| } func newSitesMatrixContentBenchmarkBuilder(t testing.TB, numPages int, skipRender, multipleDimensions bool) *hugolib.IntegrationTestBuilder { - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- disableKinds = ["rss", "sitemap", "section", "taxonomy", "term"] defaultContentLanguage = "en" @@ -1069,10 +1070,10 @@ title = "Benchmark" weight = 1 -` +`) if multipleDimensions { - files += ` + files.WriteString(` [languages.nn] weight = 2 [languages.sv] @@ -1087,10 +1088,10 @@ weight = 3 weight = 300 [roles.member] weight = 200 -` +`) } - files += ` + files.WriteString(` [[module.mounts]] source = 'content' @@ -1101,31 +1102,31 @@ versions = ["**"] roles = ["**"] -- layouts/all.html -- All. {{ .Title }}| -` +`) if multipleDimensions { - files += ` + files.WriteString(` Rotate(language): {{ with .Rotate "language" }}{{ range . }}{{ template "printp" . }}|{{ end }}{{ end }}$ Rotate(version): {{ with .Rotate "version" }}{{ range . }}{{ template "printp" . }}|{{ end }}{{ end }}$ Rotate(role): {{ with .Rotate "role" }}{{ range . }}{{ template "printp" . }}|{{ end }}{{ end }}$ {{ define "printp" }}{{ .RelPermalink }}:{{ with .Site }}{{ template "prints" . }}{{ end }}{{ end }} {{ define "prints" }}/l:{{ .Language.Name }}/v:{{ .Version.Name }}/r:{{ .Role.Name }}{{ end }} -` +`) } for i := range numPages { - files += fmt.Sprintf(` + files.WriteString(fmt.Sprintf(` -- content/p%d/index.md -- --- title: "P%d" --- -`, i+1, i+1) +`, i+1, i+1)) } b := hugolib.NewIntegrationTestBuilder( hugolib.IntegrationTestConfig{ T: t, - TxtarString: files, + TxtarString: files.String(), BuildCfg: hugolib.BuildCfg{ SkipRender: skipRender, }, diff --git a/hugolib/taxonomy_test.go b/hugolib/taxonomy_test.go index 72beb587e..c66309616 100644 --- a/hugolib/taxonomy_test.go +++ b/hugolib/taxonomy_test.go @@ -15,6 +15,7 @@ package hugolib import ( "fmt" + "strings" "testing" "github.com/gohugoio/hugo/resources/kinds" @@ -144,7 +145,8 @@ categories: ["This is Cool", "And new" ] Content. ` - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "http://example.com" -- layouts/home.html -- @@ -161,13 +163,13 @@ baseURL = "http://example.com"
  • {{ .Page.Title }} {{ .Count }}
  • {{ end }} -` +`) for i := range 10 { - files += fmt.Sprintf("\n-- content/page%d.md --\n%s", i+1, pageContent) + files.WriteString(fmt.Sprintf("\n-- content/page%d.md --\n%s", i+1, pageContent)) } - b := Test(t, files) + b := Test(t, files.String()) b.AssertFileContent("public/index.html", `
  • Hugo Rocks! 10
  • `) b.AssertFileContent("public/categories/index.html", `
  • This Is Cool 10
  • `) @@ -845,7 +847,8 @@ func BenchmarkTaxonomiesGetTerms(b *testing.B) { createBuilder := func(b *testing.B, numPages int) *IntegrationTestBuilder { b.StopTimer() - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "https://example.com" disableKinds = ["RSS", "sitemap", "section"] @@ -856,7 +859,7 @@ List. -- layouts/single.html -- GetTerms.tags: {{ range .GetTerms "tags" }}{{ .Title }}|{{ end }} -- content/_index.md -- -` +`) tagsVariants := []string{ "tags: ['a']", @@ -871,12 +874,12 @@ GetTerms.tags: {{ range .GetTerms "tags" }}{{ .Title }}|{{ end }} for i := 1; i < numPages; i++ { tags := tagsVariants[i%len(tagsVariants)] - files += fmt.Sprintf("\n-- content/posts/p%d.md --\n---\n%s\n---", i+1, tags) + files.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n---\n%s\n---", i+1, tags)) } cfg := IntegrationTestConfig{ T: b, - TxtarString: files, + TxtarString: files.String(), } bb := NewIntegrationTestBuilder(cfg) diff --git a/internal/warpc/webp.go b/internal/warpc/webp.go index a241324d5..a3ca19762 100644 --- a/internal/warpc/webp.go +++ b/internal/warpc/webp.go @@ -141,7 +141,7 @@ func (d *WebpCodec) Decode(r io.Reader) (image.Image, error) { frameSize := stride * h frames := make([]image.Image, len(destination.Bytes())/frameSize) - for i := 0; i < len(frames); i++ { + for i := range frames { frameBytes := destination.Bytes()[i*frameSize : (i+1)*frameSize] frameImg := &image.NRGBA{ Pix: frameBytes, @@ -166,10 +166,10 @@ func (d *WebpCodec) Decode(r io.Reader) (image.Image, error) { rgbData := destination.Bytes() nrgbaStride = w * 4 pix = make([]byte, nrgbaStride*h) - for y := 0; y < h; y++ { + for y := range h { srcIdx := y * stride dstIdx := y * nrgbaStride - for x := 0; x < w; x++ { + for range w { pix[dstIdx+0] = rgbData[srcIdx+0] // R pix[dstIdx+1] = rgbData[srcIdx+1] // G pix[dstIdx+2] = rgbData[srcIdx+2] // B diff --git a/markup/goldmark/goldmark_integration_test.go b/markup/goldmark/goldmark_integration_test.go index a6fea53cf..b1c182034 100644 --- a/markup/goldmark/goldmark_integration_test.go +++ b/markup/goldmark/goldmark_integration_test.go @@ -232,7 +232,8 @@ LINE8 } func BenchmarkRenderHooks(b *testing.B) { - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- -- layouts/_markup/render-heading.html -- @@ -243,7 +244,7 @@ func BenchmarkRenderHooks(b *testing.B) { {{ .Text }} -- layouts/single.html -- {{ .Content }} -` +`) content := ` @@ -271,12 +272,12 @@ D. ` for i := 1; i < 100; i++ { - files += fmt.Sprintf("\n-- content/posts/p%d.md --\n"+content, i+1) + files.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n"+content, i+1)) } cfg := hugolib.IntegrationTestConfig{ T: b, - TxtarString: files, + TxtarString: files.String(), } for b.Loop() { @@ -288,7 +289,8 @@ D. } func BenchmarkCodeblocks(b *testing.B) { - filesTemplate := ` + var filesTemplate strings.Builder + filesTemplate.WriteString(` -- hugo.toml -- [markup] [markup.highlight] @@ -305,7 +307,7 @@ func BenchmarkCodeblocks(b *testing.B) { tabWidth = 4 -- layouts/single.html -- {{ .Content }} -` +`) content := ` @@ -325,7 +327,7 @@ FENCE content = strings.ReplaceAll(content, "FENCE", "```") for i := 1; i < 100; i++ { - filesTemplate += fmt.Sprintf("\n-- content/posts/p%d.md --\n"+content, i+1) + filesTemplate.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n"+content, i+1)) } runBenchmark := func(files string, b *testing.B) { @@ -343,11 +345,11 @@ FENCE } b.Run("Default", func(b *testing.B) { - runBenchmark(filesTemplate, b) + runBenchmark(filesTemplate.String(), b) }) b.Run("Hook no higlight", func(b *testing.B) { - files := filesTemplate + ` + files := filesTemplate.String() + ` -- layouts/_markup/render-codeblock.html -- {{ .Inner }} ` diff --git a/navigation/menu_cache_test.go b/navigation/menu_cache_test.go index a1f56b416..98e98c442 100644 --- a/navigation/menu_cache_test.go +++ b/navigation/menu_cache_test.go @@ -55,9 +55,7 @@ func TestMenuCache(t *testing.T) { } for range 100 { - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { for k, menu := range testMenuSets { l1.Lock() m, ca := c1.get("k1", nil, menu) @@ -76,7 +74,7 @@ func TestMenuCache(t *testing.T) { c.Assert(m3, qt.Not(qt.IsNil)) c.Assert("changed", qt.Equals, m3[0].Title) } - }() + }) } wg.Wait() } diff --git a/related/inverted_index_test.go b/related/inverted_index_test.go index c44be4a62..b33d42955 100644 --- a/related/inverted_index_test.go +++ b/related/inverted_index_test.go @@ -17,6 +17,7 @@ import ( "context" "fmt" "math/rand" + "strings" "testing" "time" @@ -31,15 +32,16 @@ type testDoc struct { } func (d *testDoc) String() string { - s := "\n" + var s strings.Builder + s.WriteString("\n") for k, v := range d.keywords { - s += k + ":\t\t" + s.WriteString(k + ":\t\t") for _, vv := range v { - s += " " + vv.String() + s.WriteString(" " + vv.String()) } - s += "\n" + s.WriteString("\n") } - return s + return s.String() } func (d *testDoc) Name() string { diff --git a/related/related_integration_test.go b/related/related_integration_test.go index 19a31acd2..fe625eba5 100644 --- a/related/related_integration_test.go +++ b/related/related_integration_test.go @@ -16,6 +16,7 @@ package related_test import ( "fmt" "math/rand" + "strings" "testing" "github.com/gohugoio/hugo/hugolib" @@ -134,7 +135,8 @@ Related 2: 2 } func BenchmarkRelatedSite(b *testing.B) { - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = "http://example.com/" disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"] @@ -151,7 +153,7 @@ disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"] weight = 30 -- layouts/single.html -- Len related: {{ site.RegularPages.Related . | len }} -` +`) createContent := func(n int) string { base := `--- @@ -168,12 +170,12 @@ keywords: ['k%d'] } for i := 1; i < 100; i++ { - files += fmt.Sprintf("\n-- content/posts/p%d.md --\n"+createContent(i+1), i+1) + files.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n"+createContent(i+1), i+1)) } cfg := hugolib.IntegrationTestConfig{ T: b, - TxtarString: files, + TxtarString: files.String(), } for b.Loop() { diff --git a/resources/images/color.go b/resources/images/color.go index c7f3b9eb6..47c2848b9 100644 --- a/resources/images/color.go +++ b/resources/images/color.go @@ -172,11 +172,11 @@ func hexStringToColorGo(s string) (color.Color, error) { s = strings.ToLower(s) if len(s) == 3 || len(s) == 4 { - var v string + var v strings.Builder for _, r := range s { - v += string(r) + string(r) + v.WriteString(string(r) + string(r)) } - s = v + s = v.String() } // Standard colors. diff --git a/resources/page/page_generate/generate_page_wrappers.go b/resources/page/page_generate/generate_page_wrappers.go index d720b8a42..87e766a7e 100644 --- a/resources/page/page_generate/generate_page_wrappers.go +++ b/resources/page/page_generate/generate_page_wrappers.go @@ -19,6 +19,7 @@ import ( "os" "path/filepath" "reflect" + "strings" "github.com/gohugoio/hugo/codegen" "github.com/gohugoio/hugo/resources/page" @@ -41,7 +42,7 @@ const header = `// Copyright 2019 The Hugo Authors. All rights reserved. ` var ( - pageInterface = reflect.TypeOf((*page.PageMetaProvider)(nil)).Elem() + pageInterface = reflect.TypeFor[page.PageMetaProvider]() packageDir = filepath.FromSlash("resources/page") ) @@ -105,10 +106,11 @@ func importsString(imps []string) string { return fmt.Sprintf("import %q", imps[0]) } - impsStr := "import (\n" + var impsStr strings.Builder + impsStr.WriteString("import (\n") for _, imp := range imps { - impsStr += fmt.Sprintf("%q\n", imp) + impsStr.WriteString(fmt.Sprintf("%q\n", imp)) } - return impsStr + ")" + return impsStr.String() + ")" } diff --git a/resources/page/pages_cache_test.go b/resources/page/pages_cache_test.go index 6cbc71d10..44e540bac 100644 --- a/resources/page/pages_cache_test.go +++ b/resources/page/pages_cache_test.go @@ -46,9 +46,7 @@ func TestPageCache(t *testing.T) { } for range 100 { - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { for k, pages := range testPageSets { l1.Lock() p, ca := c1.get("k1", nil, pages) @@ -67,7 +65,7 @@ func TestPageCache(t *testing.T) { c.Assert(p3, qt.Not(qt.IsNil)) c.Assert("changed", qt.Equals, p3[0].(*testPage).description) } - }() + }) } wg.Wait() } diff --git a/resources/page/path_integration_test.go b/resources/page/path_integration_test.go index 5393f1d0e..bd213267f 100644 --- a/resources/page/path_integration_test.go +++ b/resources/page/path_integration_test.go @@ -59,7 +59,8 @@ title: p#2 func TestMiscPathIssues(t *testing.T) { t.Parallel() - filesTemplate := ` + var filesTemplate strings.Builder + filesTemplate.WriteString(` -- hugo.toml -- uglyURLs = false @@ -98,7 +99,7 @@ title: tags --- title: red --- -` +`) templates := []string{ "layouts/home.html", @@ -116,10 +117,10 @@ title: red const code string = "TITLE: {{ .Title }} | AOFRP: {{ range .AlternativeOutputFormats }}{{ .RelPermalink }}{{ end }} | TEMPLATE: {{ templates.Current.Name }}" for _, template := range templates { - filesTemplate += "-- " + template + " --\n" + code + "\n" + filesTemplate.WriteString("-- " + template + " --\n" + code + "\n") } - files := filesTemplate + files := filesTemplate.String() b := hugolib.Test(t, files) @@ -137,7 +138,7 @@ title: red b.AssertFileContent("public/print/tags/index.txt", "TITLE: tags | AOFRP: /tags/ | TEMPLATE: taxonomy.print.txt") b.AssertFileContent("public/print/tags/red/index.txt", "TITLE: red | AOFRP: /tags/red/ | TEMPLATE: term.print.txt") - files = strings.ReplaceAll(filesTemplate, "uglyURLs = false", "uglyURLs = true") + files = strings.ReplaceAll(filesTemplate.String(), "uglyURLs = false", "uglyURLs = true") b = hugolib.Test(t, files) // uglyURLs: true, outputFormat: html diff --git a/tpl/collections/apply.go b/tpl/collections/apply.go index 23c6a7497..ca8b0075b 100644 --- a/tpl/collections/apply.go +++ b/tpl/collections/apply.go @@ -64,7 +64,7 @@ func (ns *Namespace) Apply(ctx context.Context, c any, fname string, args ...any } } -var typeOfReflectValue = reflect.TypeOf(reflect.Value{}) +var typeOfReflectValue = reflect.TypeFor[reflect.Value]() func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...any) (reflect.Value, error) { num := fn.Type().NumIn() diff --git a/tpl/collections/collections.go b/tpl/collections/collections.go index 749ed612f..04840f8c4 100644 --- a/tpl/collections/collections.go +++ b/tpl/collections/collections.go @@ -121,7 +121,7 @@ func (ns *Namespace) Delimit(ctx context.Context, l, sep any, last ...any) (stri return "", errors.New("can't iterate over a nil value") } - var str string + var str strings.Builder switch lv.Kind() { case reflect.Map: sortSeq, err := ns.Sort(ctx, l) @@ -139,11 +139,11 @@ func (ns *Namespace) Delimit(ctx context.Context, l, sep any, last ...any) (stri } switch { case i == lv.Len()-2 && dLast != nil: - str += valStr + *dLast + str.WriteString(valStr + *dLast) case i == lv.Len()-1: - str += valStr + str.WriteString(valStr) default: - str += valStr + d + str.WriteString(valStr + d) } } @@ -151,7 +151,7 @@ func (ns *Namespace) Delimit(ctx context.Context, l, sep any, last ...any) (stri return "", fmt.Errorf("can't iterate over %T", l) } - return str, nil + return str.String(), nil } // Dictionary creates a new map from the given parameters by @@ -621,7 +621,7 @@ func (i *intersector) handleValuePair(l1vv, l2vv reflect.Value) { if err1 == nil && err2 == nil && f1 == f2 { i.appendIfNotSeen(l1vv) } - case kind == reflect.Ptr, kind == reflect.Struct: + case kind == reflect.Pointer, kind == reflect.Struct: if types.Unwrapv(l1vv.Interface()) == types.Unwrapv(l2vv.Interface()) { i.appendIfNotSeen(l1vv) } @@ -701,7 +701,7 @@ func (ns *Namespace) Union(l1, l2 any) (any, error) { if err == nil { ins.appendIfNotSeen(l2vv) } - case kind == reflect.Interface, kind == reflect.Struct, kind == reflect.Ptr: + case kind == reflect.Interface, kind == reflect.Struct, kind == reflect.Pointer: ins.appendIfNotSeen(l2vv) } diff --git a/tpl/collections/collections_integration_test.go b/tpl/collections/collections_integration_test.go index 750ea7cd8..5f5ce10dd 100644 --- a/tpl/collections/collections_integration_test.go +++ b/tpl/collections/collections_integration_test.go @@ -16,6 +16,7 @@ package collections_test import ( "context" "fmt" + "strings" "testing" "github.com/gohugoio/hugo/hugolib" @@ -568,17 +569,18 @@ title: Page%04d --- ` - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- -- layouts/all.html -- All. -` +`) - for i := 0; i < 500; i++ { - files += fmt.Sprintf(pageTemplate, i, i) + for i := range 500 { + files.WriteString(fmt.Sprintf(pageTemplate, i, i)) } - bb := hugolib.Test(b, files, hugolib.TestOptWithConfig(func(conf *hugolib.IntegrationTestConfig) { + bb := hugolib.Test(b, files.String(), hugolib.TestOptWithConfig(func(conf *hugolib.IntegrationTestConfig) { conf.BuildCfg = hugolib.BuildCfg{ SkipRender: true, } diff --git a/tpl/collections/index.go b/tpl/collections/index.go index 999abf829..6209a7e58 100644 --- a/tpl/collections/index.go +++ b/tpl/collections/index.go @@ -138,7 +138,7 @@ func prepareArg(value reflect.Value, argType reflect.Type) (reflect.Value, error // Copied from Go stdlib src/text/template/exec.go. func canBeNil(typ reflect.Type) bool { switch typ.Kind() { - case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice: return true } return false diff --git a/tpl/collections/reflect_helpers.go b/tpl/collections/reflect_helpers.go index 14b10c32f..56634fd42 100644 --- a/tpl/collections/reflect_helpers.go +++ b/tpl/collections/reflect_helpers.go @@ -108,7 +108,7 @@ func newSliceElement(items any) any { switch tp.Kind() { case reflect.Array, reflect.Slice: tp = tp.Elem() - if tp.Kind() == reflect.Ptr { + if tp.Kind() == reflect.Pointer { tp = tp.Elem() } diff --git a/tpl/partials/partials_integration_test.go b/tpl/partials/partials_integration_test.go index 022d2474f..04ea82bfd 100644 --- a/tpl/partials/partials_integration_test.go +++ b/tpl/partials/partials_integration_test.go @@ -205,7 +205,8 @@ D1 // gobench --package ./tpl/partials func BenchmarkIncludeCached(b *testing.B) { - files := ` + var files strings.Builder + files.WriteString(` -- hugo.toml -- baseURL = 'http://example.com/' -- layouts/home.html -- @@ -228,15 +229,15 @@ ABCDE {{ end }} -` +`) for i := 1; i < 100; i++ { - files += fmt.Sprintf("\n-- content/p%d.md --\n---\ntitle: page\n---\n"+strings.Repeat("FOO ", i), i) + files.WriteString(fmt.Sprintf("\n-- content/p%d.md --\n---\ntitle: page\n---\n"+strings.Repeat("FOO ", i), i)) } cfg := hugolib.IntegrationTestConfig{ T: b, - TxtarString: files, + TxtarString: files.String(), } for b.Loop() { diff --git a/tpl/strings/truncate.go b/tpl/strings/truncate.go index 0a9f5c262..66811c74e 100644 --- a/tpl/strings/truncate.go +++ b/tpl/strings/truncate.go @@ -126,9 +126,10 @@ func (ns *Namespace) Truncate(s any, options ...any) (template.HTML, error) { } else { endTextPos = lastWordIndex } - out := text[0:endTextPos] + var out strings.Builder + out.WriteString(text[0:endTextPos]) if isHTML { - out += ellipsis + out.WriteString(ellipsis) // Close out any open HTML tags var currentTag *htmlTag for i := len(tags) - 1; i >= 0; i-- { @@ -141,15 +142,15 @@ func (ns *Namespace) Truncate(s any, options ...any) (template.HTML, error) { } if tag.openTag { - out += ("") + out.WriteString(("")) } else { currentTag = &tag } } - return template.HTML(out), nil + return template.HTML(out.String()), nil } - return template.HTML(html.EscapeString(out) + ellipsis), nil + return template.HTML(html.EscapeString(out.String()) + ellipsis), nil } } diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index 43a8bdfbe..f6a72d22a 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -83,7 +83,7 @@ func (t *templateExecHelper) GetMapValue(ctx context.Context, tmpl texttemplate. return v, v.IsValid() } -var typeParams = reflect.TypeOf(hmaps.Params{}) +var typeParams = reflect.TypeFor[hmaps.Params]() func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) { if strings.EqualFold(name, "mainsections") && receiver.Type() == typeParams && receiver.Pointer() == t.siteParams.Pointer() {