

[Golang] XMLHttpRequest (XHR) HTTP GET JSON Data by GopherJS
source link: https://siongui.github.io/2016/01/20/go-xhr-http-get-json-by-gopherjs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction
It is really cool to run Go code in the browser. GopherJS is a compiler from Go to JavaScript, which makes this possible. In this post, we will show how to use XMLHttpRequest (XHR) to send HTTP GET requests to retrieve JSON data from remote server.
Install GopherJS
Run the following command to install GopherJS:
$ go get -u github.com/gopherjs/gopherjs
Source Code
First we write a simple HTML for our demo: (index.html)
<!doctype html> <html> <head> <title>XHR HTTP Get by GopherJS</title> </head> <body> <script src="demo.js"></script> </body> </html>
The following is the JSON data to be retrieved by HTTP GET request:
sukhada.json | repository | view raw{"data": [["\u25ce\u3000\u300aConcise Pali-English Dictionary\u300b by A.P. Buddhadatta Mahathera", "sukhada", ": [adj.] producing happiness."]], "word": "sukhada"}
It is surprising easy to send HTTP GET XHR request: Use Golang built-in net/http package! You just use Get method as usual, and GopherJS will take care of all the rests for you!
xhrget2.go | repository | view rawpackage main import "net/http" import "encoding/json" type Exp [3]string type Def struct { Data []Exp Word string } func main() { resp, err := http.Get("/sukhada.json") if err != nil { // handle error println(err) } defer resp.Body.Close() w := Def{} dec := json.NewDecoder(resp.Body) err2 := dec.Decode(&w) if err2 != nil { // handle error println(err2) } println(w.Word) println(w.Data[0]) }
Compile the Go code to JavaScript by:
$ gopherjs build xhrget.go -o demo.js
Put demo.js together with the index.html and sukhada.json in the same directory. You need a simple HTTP server to run this demo. Use GopherJS serve command to serve the above files, and open your browser console to see the output.
Tested on: Ubuntu Linux 15.10, Go 1.5.3.
References:
[2]Bindings · gopherjs/gopherjs Wiki · GitHub[3]xhr - GoDoc[4]http - The Go Programming Language[5]delete xhr/transport, GopherJS has its own now. · dominikh/go-js-xhr@00e3346 · GitHub[6]go json decode array[7]How to Unmarshal a JSON Array of Arrays in Go - Fabio Berger[8]encoding/json - The Go Programming Language
Recommend
-
7
Not FoundYou just hit a route that doesn't exist... the sadness.LoginRadius empowers businesses to deliver a delightful customer experience and win customer trust. Using the LoginRadius Identity...
-
6
[Go WebAssembly] XMLHttpRequest (XHR) October 19, 2018 This post shows how to...
-
11
[Vue.js] AJAX XMLHttpRequest (XHR) Example March 22, 2017 Examp...
-
7
Introduction Access HTTP Request Header in Golang code running on your browser. This can be done by
-
6
GopherJS XMLHttpRequest (XHR) and MakeFunc Example February 18, 2016...
-
8
Source Code If you have following element in HTML: <div id="foo" data-my-demo-value="Hey"></div> Access the value by Dataset() function, which returns map[string]string:...
-
7
[GopherJS] Setting Implementation via JSON and Web Storage (localStorage) Ja...
-
21
This post shows how to interact with servers from browsers. Send data to URL or retrieve data from URL from your browser via XMLHttpRequest method in Go/GopherJS. The full code exam...
-
21
Introduction It is really cool to run Go code in the browser. GopherJS is a compiler from Go to
-
6
Closed Bug 1016808...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK