{"id":6278,"date":"2022-09-02T12:28:27","date_gmt":"2022-09-02T06:58:27","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/"},"modified":"2022-09-02T12:28:27","modified_gmt":"2022-09-02T06:58:27","slug":"solved-need-to-send-golang-data-to-html-by-button-click","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/","title":{"rendered":"[Solved] need to send golang data to html by button click"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-67790714\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"67790714\" data-parentid=\"67789372\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You probably put very little effort into figuring this out. Though I am bored so I decided to help you out. Here is your backend:<\/p>\n<pre><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"log\"\n    \"net\/http\"\n    \"time\"\n)\n\ntype Response struct {\n    CurrentTime string\n}\n\nfunc main() {\n    http.Handle(\"\/\", http.FileServer(http.Dir(\"web\")))\n\n    http.HandleFunc(\"\/get-time\", func(rw http.ResponseWriter, r *http.Request) {\n        ctime := Response{\n            CurrentTime: time.Now().Format(time.RFC3339),\n        }\n        byteArray, err := json.Marshal(ctime)\n        if err != nil {\n            fmt.Println(err)\n        }\n        rw.Write(byteArray)\n    })\n\n    if err := http.ListenAndServe(\":5000\", nil); err != nil {\n        log.Fatal(err)\n    }\n}\n\n<\/code><\/pre>\n<p>And you also need frontend located in (web\/index.html):<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;Document&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;button id=\"gt\"&gt;get time&lt;\/button&gt;\n    &lt;div id=\"res\"&gt;&lt;\/div&gt;\n    &lt;script&gt;\n        document.getElementById(\"gt\").onclick = async (e) =&gt; {\n            var req = await fetch(\"\\get-time\").then(r =&gt; r.json())\n            document.getElementById(\"res\").innerHTML = req.CurrentTime\n        }\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved need to send golang data to html by button click <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You probably put very little effort into figuring this out. Though I am bored so I decided to help you out. Here is your backend: package main import ( &#8220;encoding\/json&#8221; &#8220;fmt&#8221; &#8220;log&#8221; &#8220;net\/http&#8221; &#8220;time&#8221; ) type Response struct { CurrentTime string } func main() { http.Handle(&#8220;\/&#8221;, http.FileServer(http.Dir(&#8220;web&#8221;))) http.HandleFunc(&#8220;\/get-time&#8221;, func(rw http.ResponseWriter, r *http.Request) { ctime &#8230; <a title=\"[Solved] need to send golang data to html by button click\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/\" aria-label=\"More on [Solved] need to send golang data to html by button click\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[334,596,346],"class_list":["post-6278","post","type-post","status-publish","format-standard","hentry","category-solved","tag-ajax","tag-go","tag-html"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] need to send golang data to html by button click - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] need to send golang data to html by button click - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You probably put very little effort into figuring this out. Though I am bored so I decided to help you out. Here is your backend: package main import ( &quot;encoding\/json&quot; &quot;fmt&quot; &quot;log&quot; &quot;net\/http&quot; &quot;time&quot; ) type Response struct { CurrentTime string } func main() { http.Handle(&quot;\/&quot;, http.FileServer(http.Dir(&quot;web&quot;))) http.HandleFunc(&quot;\/get-time&quot;, func(rw http.ResponseWriter, r *http.Request) { ctime ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-02T06:58:27+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] need to send golang data to html by button click\",\"datePublished\":\"2022-09-02T06:58:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/\"},\"wordCount\":59,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"ajax\",\"go\",\"html\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/\",\"name\":\"[Solved] need to send golang data to html by button click - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-02T06:58:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] need to send golang data to html by button click\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] need to send golang data to html by button click - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] need to send golang data to html by button click - JassWeb","og_description":"[ad_1] You probably put very little effort into figuring this out. Though I am bored so I decided to help you out. Here is your backend: package main import ( \"encoding\/json\" \"fmt\" \"log\" \"net\/http\" \"time\" ) type Response struct { CurrentTime string } func main() { http.Handle(\"\/\", http.FileServer(http.Dir(\"web\"))) http.HandleFunc(\"\/get-time\", func(rw http.ResponseWriter, r *http.Request) { ctime ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/","og_site_name":"JassWeb","article_published_time":"2022-09-02T06:58:27+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] need to send golang data to html by button click","datePublished":"2022-09-02T06:58:27+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/"},"wordCount":59,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["ajax","go","html"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/","url":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/","name":"[Solved] need to send golang data to html by button click - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-02T06:58:27+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-need-to-send-golang-data-to-html-by-button-click\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] need to send golang data to html by button click"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/6278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=6278"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/6278\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=6278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=6278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=6278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}