Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Is it posible to return javascript function from golang struct?

Example below is a golang struct

type Column struct {
        Data            string      `json:"data"`
        Title           string      `json:"title"`
        Type            string      `json:"type"`
        Class           string      `json:"class"`
        Visible         bool        `json:"visible"`
        Render          template.JS `json:"render"`
}

func (c *Column) SetValue() {
        // code below is flexible depend on condition but here i keep it simple.
        c.Render = template.JS(`function(data, type, row) { if(type === 'display'){ return $.fn.dataTable.render.text().display(data);} return data;}`);
}

Here is Javascript in golang template

<script>
    $(function () {
        console.log({{.Columns}}, wantedobj);
    });
</script>

Here is chrome developer tools.

  • left list is format value from struct above.
  • right list is format that I want.

enter image description here

on render is there any posible way to get javascript function instead of string? (please see render at right picture)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using template.JS is correct. Your problem is that in HTML templates, values are escaped based on contexts:

At parse time each {{.}} is overwritten to add escaping functions as necessary.

Inside <script> tags, struct values such as {{.Columns}} are rendered as JSON. Now, since template.JS is simply a defined type over string, when you marshal Columns as JSON, the field Render keeps its string representation.

If you print Render directly, it will appear as unquoted Javascript:

console.log({{.Columns.Render}}); // unescaped JS function

Now you might think of implementing MarshalJSON on Columns to render Render as unescaped Javascript, but that wouldn't be valid JSON anymore. Function is not a JSON data type. It can only be a string. An option is to use text/template package instead, but it won't escape anything anymore. Or construct the Javascript object using the single fields:

<script>
    $(function () {
        const foo = {
            data: {{.Columns.Data}},
            render: {{.Columns.Render}}
        }
        console.log(foo);
    });
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!