insert javascript variable to mysql in go

I have javascript variable which is named x and I want this x variable’s value to insert mysql using golang. When I run this code then undefined x error shown. How do I insert javascript variable to mysql?

package main

import (

    _ ""

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, `<!DOCTYPE html>
                <meta charset="utf-8" />
                <meta http-equiv="X-UA-Compatible" content="IE=edge">
                <title>Page Title</title>
                <meta name="viewport" content="width=device-width, initial-scale=1">



var x=5;    


        db, err := sql.Open("mysql", "root:@tcp(")
        defer db.Close()
        if err != nil {
        insForm, err := db.Prepare("INSERT INTO number(number_position) VALUES(?)")
        if err != nil {

    http.ListenAndServe(":8080", nil)

A JavaScript variable (var x=5;) inside a text blob is not a variable to Go. You can not access it from Go code.

What are you trying to do? Will x always be 5? Why?

Use html/template package.

I don’t think this answers the question. But of course the question is not clear.

Oh, you are right, I thought he wants to generate HTML/Javascript :smiley:

@bashgaa, you have to send your stuff from browser to some endpoint in go either as form submit or AJAX query.

This is client server communication, you can’t do it all in one place.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.