Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda