Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda