title: Calling Golang from HTML
body: <p>I'm helping with an open source project.  It's a small Go webserver running on a device containing a Raspberry Pi.  I want to be able to have a user click a button on an html screen, which calls a routine in Go, which returns 2 values, a boolean and a string.</p>
  
  <p>What we are wanting to do is see which network interfaces are up on the raspberry pi e.g. is the lan connection up?</p>
  
  <p>To do this I really need to ping a site from each interface.  This takes a few seconds for each of 3 interfaces: Lan, WiFi, and 3G.</p>
  
  <p>I can do this when the page is requested and fill in an html template as the page loads, but it means waiting maybe 10 to 15 secs for the page to load, so it seems like something is broken.</p>
  
  <p>So I want to be able to list each of the 3 interfaces on the page and have the user click 'test' which then calls a routine in the underlying Go webserver.  </p>
  
  <p>I then need to be able to display the results from the call in a couple of text areas for each interface.</p>
  
  <p>What I have tried:</p>
  
  <p>I have tried registering a Go function (in this case IsLANConnectionUp) using funcmap from the net/html package and calling it from the html template from a JavaScript function, like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>  &lt;button onclick = "getLANStatus()" class="btn btn-primary"&gt;Test&lt;/button&gt;
   
    &lt;script&gt;
      function getLANStatus() {
          var status = document.getElementById('status');
          {{ if IsLANConnectionUp }}
            status.innerHTML = "Lan is up!"
          {{ else }}
            status.innerHTML = "Lan is down!"
          {{ end }}
      }
    &lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>But having the template code inside the javascript code doesn't seem to work.  Also, I'd like the text output from the ping command (which my Go function getLANStatus and I don't know how to extract that data from the function call.  The documentation says only one value can be returned.</p>
  
  <p>Searching on StackOverflow I see this: <a href="https://stackoverflow.com/questions/29353509/calling-golang-functions-from-within-javascript-code">calling Golang functions from within javascript code</a></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>$.ajax({
    url: "http://www.example.com/signup",
    data: {username: "whatever"} //If the request needs any data 
  }).done(function (data) {
    // Do whatever with returned data
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>But it says things like "// Do whatever with the returned data"  I'm new to web programming and so don't know how to use that code.  If this is the way to go, could someone please expand on this a little?</p>
  
  <p>Any help would be much appreciated.</p>