ArkadyB
ArkadyB

Reputation: 1275

Pass separate value into template function

I am trying to find a way to pass value into go template function. What i mean. I have template functions list defined in struct method:

func (o *MyObj) run() error{
  funcMap := template.FuncMap{
    "func1": func1,
    "func2": func2,
    }
}

Function1 signature is func func1(myval string) string{...}, but for Function2 id need to have access to one of MyObj struct field. Mean:

func func2(myval string) string{
//MyObj.field would need to be used here.. How can i do it?
}

Upvotes: 0

Views: 52

Answers (1)

matt.s
matt.s

Reputation: 1736

Credit to @JiangYD

func (o *MyObj)func2(myval string) string{}`

func (o *MyObj) run() error{
  funcMap := template.FuncMap{
    "func1": func1,
    "func2": o.func2,
    }
}

Upvotes: 0

Related Questions