title: Pass Golang Variables into Javascript
body: <p>I am trying to pass my Golang variables into a Javascript file. I have read multiple articles online, including <a href="https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript">How to pass variables and data from PHP to JavaScript?</a>. I feel like I'm pretty close, but alas the most eloquent implementation to this problem eludes me.</p>
  
  <p>I'm looking for a simple and elegant solution that <strong>injects an object value in Go with a variable in Javascript</strong>. I feel like this is a very common problem, so the lack of a clear tutorial on this surprises me. </p>
  
  <p>My first file [server.go] servers an HTML page using the templating libraries in Golang passing in a context and serving them through Handlers. </p>
  
  <pre><code>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 
   } 
  </code></pre>
  
  <p>I now have an HTML document that is served, which adds a Javascript file to it. </p>
  
  <pre><code> &lt;html&gt;
       &lt;head&gt;
       //import javascript file 
       &lt;/head&gt;
       &lt;body&gt;
       //some body
       &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p>Finally, the javascript file is what I am really interested in: </p>
  
  <pre><code>  var data; //some 
    var number1;
    var number2;
  
    function doSomething(){
    }
  </code></pre>
  
  <p>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. </p>