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.