1

I am experimenting with Go's templates.

I am curious: is it possible to do the following: {{$myVariable := randomStringFunc | saveFunc}}

Where:
$myVariable - randomly chosen variable name
randomStringFunc - function that generates random strings
TRICKY: saveFunc - a function that saves the variable name and its value

I have looked into capture-or-assign-golang-template-output-to-variable but I am not sure how and if it can help me achieve my goal.

EDIT In the end I would like to have a mapping between the variable name that is defined in the template and the value that is assigned to it:

var variableMapping map[string]string
After template execution the content of variableMapping should be something like:

{
"$myVariable:"randomString1",
"$anotherVariable":"5",
"$thirdVariableInMyTemplate":"false"
}
Community
  • 1
  • 1
NewestStackOverflowUser
  • 2,792
  • 5
  • 22
  • 31
  • If i understand you correctly it seems to me that you can, but best to try it out yourself https://play.golang.org/p/CMSu3-Mo_a. Of course your `saveFunc` can then return either the output from `randomStringFunc` or a new value and that is what will be assigned to `myVariable`. – mkopriva Mar 30 '17 at 11:16
  • Your assumption is almost right. The only thing that is missing is getting hold of the name of the variable. – NewestStackOverflowUser Mar 30 '17 at 11:36
  • 1
    Ah sorry, now I understand what you mean... but I don't think that's possible with what the Go template engine is able to do. – mkopriva Mar 30 '17 at 11:43

0 Answers0