How to execute go function on javascript?

I have a function like this
func Hello(){
log.Printf(“Hello world”)
}
All I want is when the user press a button on my template it execute Hello function from Go
Like this
function SubmitFunction() {
//EXECUTE GO FUNCTION
document.getElementById(“myForm”).submit();
}

Can you be more specific? Are you running a web service written in Go and you want to execute a template and return the result to the user or are you actually running Go in the browser, e.g. with WASM, and want to invoke the function via an export from WASM to JavaScript?

Yes, i’m running like a server on my pc, and I want to execute a function, it doesn’t matter what it does. I wrote log.Printf just for example

It sounds like you want to post a form to a go API? I would check out https://gowebexamples.com/ as a starting point. Specifically the forms section, which illustrates how to do what I think you’re asking how to do.

I do this by adding an endpoint to the Go Web Server. Instead of open a html page you fire a function.

switch path {
  case "function":
    hello(w, r)
  case "": 
    tpl.ExecuteTemplate(w, "home.html", url)
  default:
    tpl.ExecuteTemplate(w, page, url)
}
1 Like