-1

I am looking for simple method to convert simple template with ${myvar} to GO template with {{ myvar }}.

Is there any library to achieve that?

Jon Saw
  • 7,599
  • 6
  • 48
  • 57
  • If this is a matter of converting text, then why would you not use one of the many available scripting languages that have good string facilities or even the venerable AWK program to process the text? – andy mango Jun 25 '17 at 22:43
  • It still won't work afterward anyway. `{{myvar}}` is not a legitimate reference in a Go template; in Go it would be `{{.MyVar}}`, because it must start with the root context (`.` prefix) and be an exported (capitalized) field (unless it is a `map`, in which case it may be any case, but must still start with dot). – Adrian Jun 26 '17 at 20:46

2 Answers2

0

Use regex find \${([a-z0-9\_\-]+)} and replace with {{\1}}

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
0

You can actually configure your own delimiters. See here: https://golang.org/pkg/text/template/#Template.Delims

So, you should be able to configure prefix as "${" and suffix as "}". Then you should be able to keep using ${myvar} format.

If you prefer to use a regexp to find and replace them, then you regexp should be a little bit more complicated. At the very least it should allow spaces and dots in addition to those proposed. But I'd suggest investigating non greedy replacements instead. See here for an example.

Seva
  • 2,388
  • 10
  • 9