jquery - Pass Golang Variables into Javascript -
i trying pass golang variables javascript file. have read multiple articles online, including how pass variables , data php javascript?. feel i'm pretty close, alas eloquent implementation problem eludes me.
i'm looking simple , elegant solution injects object value in go variable in javascript. feel common problem, lack of clear tutorial on surprises me.
my first file [server.go] servers html page using templating libraries in golang passing in context , serving them through handlers.
type passstruct struct{ data []int number1 int number2 int } //inject structure render func render(w http.responsewriter, tmpl string, context context){ //parse context , fill in templating variables using template }
i have html document served, adds javascript file it.
<html> <head> //import javascript file </head> <body> //some body </body> </html>
finally, javascript file interested in:
var data; //some var number1; var number2; function dosomething(){ }
i need send passstruct javascript file, , map out each of values within structure variable in javascript file. have tried ajax calls, have not had success far.
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; }
Comments
Post a Comment