Open
Description
The IsTrue
, which is used in templates to determine if if
and when
etc. conditionals should be invoked makes sense in most situations. Some examples:
package main
import (
"fmt"
"text/template"
"time"
)
func main() {
type s struct{}
printIsTrue(1) // true
printIsTrue(0) // false
printIsTrue(-1) // true
printIsTrue(s{}) // true
printIsTrue(&s{}) // true
printIsTrue((*s)(nil)) // false
printIsTrue(nil) // false
printIsTrue("") // false
printIsTrue("foo") // true
printIsTrue(time.Now()) // true
printIsTrue(time.Time{}) // true
}
func printIsTrue(in interface{}) {
b, _ := template.IsTrue(in)
fmt.Println(b)
}
My main challenge with the above is that Struct values are always true
-- even the zero time.Time
value above.
It would be useful if the above method could check some additional truther
interface:
type truther interface {
IsTrue() bool
}
If the above is considered a breaking change, an alternative suggestion would be to make the template.IsTrue
function settable.