I need some javascript code inside go code.
package main
import (
"fmt"
"net/http"
)
func work(w http.ResponseWriter, r *http.Request){
fmt.Printf("<script>console.log('javascript working')</script>")
}
func main() {
http.HandleFunc("/", work)
http.ListenAndServe(":4000", nil)
}
Running it in browser and see browser's console window there is nothing to see. How do I run javascript code in go code? Because using ajax with golang it is difficult.
Very simple javascript code running in go code is
package main
import (
"fmt"
"net/http"
)
func main() {
hello := "hello yu bn"
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `<html>
<head>
</head>
<body>
<h1>Go Timer (ticks every second!)</h1>
<div id="output"></div>
<script type="text/javascript">
console.log("`+hello+`");
</script>
</body>
</html>`)
})
http.ListenAndServe(":9000", nil)
}
it is nice! Check browser console!