FAQ
I have a `map` of values that looks like this:

     vals := map[string]interface{}{"foo": 1, "bar": 2, "baz": 7}
     data := map[string]interface{}{"bat": "obj", "values": vals}

What should my template look like to generate the following string (note
the correct comma usage)?

     SET obj.foo=1, obj.bar=2, obj.baz=7

I started with this as my template:

     SET {{range $i, $v := .values}} {{.bat}}.{{$i}}={{$v}},{{end}}

But that just prints out

     SET

And even if that did work, the commas would be incorrect. I then tried to
use a custom function to format the map, but I couldn't get the template to
ever call my function. None of the following seemed to work:

     SET {{.MyFunction .values}}
     SET {{call .MyFunction .values}}
     SET {{call MyFunction .values}}

when MyFunction was defined as:

     func MyFunction(data map[string]interface{}) string {
       fmt.PrintLn('i was called!')
       return "foo"
     }

And I'm executing the templates using a helper function that looks like
this:

     func useTemplate(name string, data interface{}) string {
       out := new(bytes.Buffer)
       templates[name].Execute(out, data)
       return string(out.Bytes())
     }

Thanks!

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • David Symonds at Sep 11, 2013 at 1:48 am

    On 11 September 2013 07:40, wrote:

    func useTemplate(name string, data interface{}) string {
    out := new(bytes.Buffer)
    templates[name].Execute(out, data)
    return string(out.Bytes())
    }
    Don't ignore the error returned by Execute. It will probably explain
    what's going wrong.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bill at Sep 11, 2013 at 2:57 am
    Execute panics when something goes wrong and you don't handle it so there
    was no way I could ignore the error. In these instances, all the errors
    were telling me was that it couldn't find my functions which was what my
    original question was about. To anyone that comes across this later, here
    is what I ended up doing:

    var funcs = template.FuncMap{
    "commaJoin": commaJoin,
    }

    t := template.New(name)
    t.Funcs(funcs)
    templates[name] = template.Must(t.Parse(`SET {{$.vals | commaJoin $.bat}}`))

    func commaJoin(parent string, m map[string]interface{}) string {
    var buf bytes.Buffer
    first := true
    for k, _ := range m {
    if !first {
    buf.WriteString(", ")
    }
    first = false
    buf.WriteString(parent)
    buf.WriteByte('.')
    buf.WriteString(k)
    buf.WriteString(" = {")
    buf.WriteString(k)
    buf.WriteByte('}')
    }
    return buf.String()
    }

    Thanks!
    On Tuesday, September 10, 2013 6:48:25 PM UTC-7, David Symonds wrote:
    On 11 September 2013 07:40, <bi...@bunkat.com <javascript:>> wrote:

    func useTemplate(name string, data interface{}) string {
    out := new(bytes.Buffer)
    templates[name].Execute(out, data)
    return string(out.Bytes())
    }
    Don't ignore the error returned by Execute. It will probably explain
    what's going wrong.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • David Symonds at Sep 11, 2013 at 3:42 am

    On 11 September 2013 12:57, wrote:

    Execute panics when something goes wrong and you don't handle it so there
    was no way I could ignore the error.
    Execute does *not* panic. It returns an error, which you were
    ignoring. If you found a way to cause Execute to panic then please
    file a bug.

    FYI, you could write your commaJoin function a bit simpler:

    func commaJoin(parent string, m map[string]interface{}) string {
       parts := make([]string, 0, len(m))
       for k := range m {
         parts = append(parts, fmt.Sprintf("%s.%s = {%s}", parent, k, k))
       }
       return strings.Join(parts, ", ")
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bill at Sep 11, 2013 at 4:21 am
    Of course you're right, it was Template.Must that panics when parsing the
    template because I couldn't figure out how to use functions correctly. I
    was never making it to Execute with the sample code that I showed. I've
    added appropriate error handing to Execute now to catch things in the
    future. Thanks for the more concise commaJoin function as well.
    On Tuesday, September 10, 2013 8:42:22 PM UTC-7, David Symonds wrote:
    On 11 September 2013 12:57, <bi...@bunkat.com <javascript:>> wrote:

    Execute panics when something goes wrong and you don't handle it so there
    was no way I could ignore the error.
    Execute does *not* panic. It returns an error, which you were
    ignoring. If you found a way to cause Execute to panic then please
    file a bug.

    FYI, you could write your commaJoin function a bit simpler:

    func commaJoin(parent string, m map[string]interface{}) string {
    parts := make([]string, 0, len(m))
    for k := range m {
    parts = append(parts, fmt.Sprintf("%s.%s = {%s}", parent, k, k))
    }
    return strings.Join(parts, ", ")
    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 11, '13 at 1:47a
activeSep 11, '13 at 4:21a
posts5
users2
websitegolang.org

2 users in discussion

Bill: 3 posts David Symonds: 2 posts

People

Translate

site design / logo © 2022 Grokbase