Skip to content

text/template: make "eq" support all comparable types #33740

@a8m

Description

@a8m

The current eq function is limited only to "basic types", and it does not support other Go types that are comparable, like pointers or structs.
Also, it's not possible to decorate it, because it's private. Setting "eq": reflect.DeepEqual won't provide the same functionally, because cases like this:

package main

import (
	"log"
	"os"
	"reflect"
	"text/template"
)

func main() {
	tmpl, err := template.New("titleTest").
		Funcs(template.FuncMap{
			"goeq": reflect.DeepEqual,
		}).
		Parse(`{{ eq .A 1 }} != {{ goeq .A 1 }}`)
	if err != nil {
		log.Fatalf("parsing: %s", err)
	}
	err = tmpl.Execute(os.Stdout, struct{ A int64 }{1})
	if err != nil {
		log.Fatalf("execution: %s", err)
	}
	// Output: true != false
}

https://play.golang.org/p/zTg33Lggq0f

My current solution is just copying the current code from text/template/funcs.go, and adding the support by myself.

I would like to hear what others think about this, because it feels like a basic functionality that is missing when working with Go types.

If this proposal will be accepted, I will be more than happy to contribute my changes.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions