'Pass Golang Variables into Javascript

I am trying to pass my Golang variables into a Javascript file. I have read multiple articles online, including How to pass variables and data from PHP to JavaScript?. I feel like I'm pretty close, but alas the most eloquent implementation to this problem eludes me.

I'm looking for a simple and elegant solution that injects an object value in Go with a variable in Javascript. I feel like this is a very common problem, so the lack of a clear tutorial on this surprises me.

My first file [server.go] servers an HTML page using the templating libraries in Golang passing in a context and serving them through Handlers.

type PassStruct struct{
    data    []int
    number1 int
    number2 int
}

//inject the structure into render
func render(w http.ResponseWriter, tmpl string, context Context){
       //parse context and fill in templating variables using template 
 } 

I now have an HTML document that is served, which adds a Javascript file to it.

 <html>
     <head>
     //import javascript file 
     </head>
     <body>
     //some body
     </body>
 </html>

Finally, the javascript file is what I am really interested in:

  var data; //some 
  var number1;
  var number2;

  function doSomething(){
  }

I need to send the PassStruct into the Javascript file, and map out each of the values within the structure to a variable in the Javascript file. I have tried AJAX calls, but have not had success so far.



Solution 1:[1]

Finally , after spending 2 days on the problem , i found an elegant way to work around it . We will use templates to solve this problem .

Read about templates usage in golang here - Understanding Templates with Examples .

Here is a sample code to pass a golang variable called golangVar in javascript .

HTML_File -

<html>
<head>
    <script>
        var golangVar = "{{.}}"
        console.log(golangVar);
    </script>
 </head>
 <body>
 <!--some body-->
 </body>
</html>

Golang HttpHandler -

Make sure you include "html/template" library in the golang code .

func golangHttpHandler(w http.ResponseWriter, r *http.Request) {
var golangVar string 
golangVar = "ProblemSolved" 
templ, err := template.ParseFiles("/home/PathToHTMLPage/HTML_File.html")
if(err!=nil){
    fmt.Println("Error = ",err)
}
err = templ.Execute(w, golangVar)
if(err!=nil){
    fmt.Println("Error = ",err)
}   
}

Also, a bonus tip - If you are working around with buffalo framework for golang , you can use default plush package for templating . Its similar to what we have done here with just a little bit of change in syntax and passing of html file . Read - Templating in Buffalo Framework

Solution 2:[2]

This way maybe helpful:

Html:

<html>
    <head>
     <!--import javascript file -->
        <script type="text/javascript" src="static/myjs.js"></script>
        <script type="text/javascript">
            myFunc({{.}});
        </script>
     </head>
     <body>
     <!--some body-->
     </body>
</html> 

Javascript:(myjs.js)

function myFunc(passStruct){
    var obj = Object();

    var data = passStruct.Data;
    var number1=passStruct.Number1;
    var number2=passStruct.Number2;

    obj.data=data;
    obj.number1=number1;
    obj.number2=number2;

    return obj;
 }

Solution 3:[3]

You can write your own template function and use it to marshal struct into json.

See my answer here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 joeyave