Add template function experimental cmd
This commit is contained in:
parent
9c299bc709
commit
1639dd787f
26
cmd/tplfn/tplfn.go
Normal file
26
cmd/tplfn/tplfn.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tplstr := "hello {{myFunc}}\n"
|
||||
|
||||
fmap := template.FuncMap{}
|
||||
fmap["myFunc"] = func() (string, error) {
|
||||
return "world", fmt.Errorf("hi")
|
||||
}
|
||||
|
||||
tpl, err := template.New("demo").Funcs(fmap).Parse(tplstr)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Error creating template: %v", err))
|
||||
}
|
||||
|
||||
err = tpl.Execute(os.Stdout, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user