Skip to content

Instantly share code, notes, and snippets.

@ondrj
Forked from Miciah/templates-strings-replace.go
Created December 10, 2020 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ondrj/1246de3c51357cfe01f52a7a17cf89f3 to your computer and use it in GitHub Desktop.
Save ondrj/1246de3c51357cfe01f52a7a17cf89f3 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"strings"
"text/template"
)
func replace(a, b, c string, n int) string {
return strings.Replace(a, b, c, n)
}
func main() {
fmt.Println(strings.Replace("foo", "f", "b", -1))
// Cannot use arbitrary functions.
//tmpl := `{{print "xyzzy"}}{{call strings.Replace "foo/bar" "/" "-" 1}}{{print "quux"}}`
//tmpl := `{{print "xyzzy"}}{{call "strings.Replace" "foo/bar" "/" "-" 1}}{{print "quux"}}`
//tmpl := `{{print "xyzzy"}}{{call replace "foo/bar" "/" "-" 1}}{{print "quux"}}`
//tmpl := `{{print "xyzzy"}}{{call "replace" "foo/bar" "/" "-" 1}}{{print "quux"}}`
// The documentation says we can use functions in "the global function map",
// but apparently that does not include strings.Replace or the replace
// function defined above:
// During execution functions are found in two function maps: first in the
// template, then in the global function map. By default, no functions are
// defined in the template but the Funcs method can be used to add them.
// https://golang.org/pkg/text/template/#hdr-Functions
// Cannot range over a string.
//tmpl := `{{range .}}{{print "x"}}{{end}}`
// Cannot range over a string this way either.
//tmpl := `{{range $i, $x := .}}{{"x"}}{{end}}`
// block does not work.
//tmpl := `{{block "f" .}}{{.}}{{end}}`
// Cannot use slice expressions with index.
//tmpl := `{{define "f"}}{{printf "%v" .}}{{end}}{{template "f" index . 1:}}`
// Cannot do arithmetic ("-" is not recognised as an operator, "add",
// "subtract", and "minus" are not recognised as functions).
//tmpl := `{{printf "%.*s" 6 (len . - 1)}}`
//tmpl := `{{printf "%.*s" 6 (add (len .) -1)}}`
//tmpl := `{{printf "%.*s" 6 (subtract (len .) -1)}}`
//tmpl := `{{printf "%.*s" 6 (add (len .) -1)}}`
// All the information I can find online suggests creating a custom function
// (which is not an option for me):
// https://www.reddit.com/r/golang/comments/38g9og/substringing_in_go_templates/
// http://stackoverflow.com/questions/19771787/golang-how-to-split-string-in-template
// http://stackoverflow.com/questions/25173549/go-templates-range-over-string
// http://stackoverflow.com/questions/25689829/arithmetic-in-go-templates
// Hey, recursion works!
//tmpl := `{{define "f"}}{{template "f"}}{{end}}{{template "f"}}`
// Hey, maybe we could do some nasty hack with len and string truncation!
tmpl := `{{printf "%.*s %v" 6 . (len .)}}`
// We can catenate strings and call len on them! We could add recursion to
// this to build up a string $x and terminate when (len $x) equals the length
// of our input.
//tmpl := `{{$x := ""}}{{$x := printf "%s%s" $x "x"}}{{printf "%d " (len $x)}}{{$x := printf "%s%s" $x "x"}}{{printf "%d " (len $x)}}{{$x := printf "%s%s" $x "x"}}{{printf "%d " (len $x)}}`
// Cannot reference a variable defined outside a template from within that
// template.
//tmpl := `{{$s := .}}{{$x := ""}}{{$n := len .}}{{define "f"}}{{$x := printf "%s%s" "x" "x"}}{{$i := len $x}}{{printf "%v %v" "blah" $i}}{{end}}{{template "f"}}`
t, err := template.New("slash-to-dash").Parse(tmpl)
if err != nil {
fmt.Printf("template.New(): %v\n", err)
os.Exit(1)
}
t.Execute(os.Stdout, "namespace/name")
//t.Execute(os.Stdout, []string{"namespace/name"})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment